Re: Division in loop

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



What are the typical values of bytes and size? Suspect
"(bytes / size)", being integer division, probably truncates
to 0 if size is relatively large...

Maybe you want "(bytes * 100) / size"? Or as another
alternative (arithmetically approximately equivalent,
but not quite as clear, however, it saves on multiply
operations):


size = size / 100;
...
...
...
printf(... bytes / size);



On 09/08/2009 21:40 +0700, Randi Botse wrote:
>>	hi all,
>>	
>>	....
>>	int fd, fdp;
>>	unsigned int size;
>>	int progress = 0;
>>	size_t bytes = 0;
>>	void *buffer;
>>	....
>>	
>>	/* open fd from a device then create fdp, allocate buffer,, etc. */
>>	
>>	while (bytes < size) {
>>	        ret = read(fd, buffer + bytes, size - bytes);
>>	        if (ret == -1) {
>>	            perror("read");
>>	            return -1;
>>	        }
>>	        ret = write(fdp, buffer + bytes, ret);
>>	        if (ret == -1) {
>>	            perror("write");
>>	            return -1;
>>	        }
>>	        bytes += ret;
>>	
>>	        progress = (int) ((bytes / size) * 100);
>>	        printf("\rcompleted: %i%%", progress);
>>	        fflush(stdout);
>>	    }
>>	
>>	....
>>	
>>	I run this loop to receive some data, the printf() seem not updated,
>>	the progress output is
>>	always in it's initial value until the loop was finished. but when i do...
>>	
>>	progress = (int) ((bytes / 2) * 100); /* change size to 2 (or random number) */
>>	
>>	then it's works... what's wrong here???
>>	--
>>	To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
>>	the body of a message to majordomo@xxxxxxxxxxxxxxx
>>	More majordomo info at  http://vger.kernel.org/majordomo-info.html
End of included message



-- 
+----------------------+
| Tim Walberg          |
| 830 Carriage Dr.     |
| Algonquin, IL 60102  |
| twalberg@xxxxxxxxxxx |
+----------------------+
--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Assembler]     [Git]     [Kernel List]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [C Programming]     [Yosemite Campsites]     [Yosemite News]     [GCC Help]

  Powered by Linux