linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Eric Bambach <bot403@cisu.net>
To: Randi Botse <nightdecoder@gmail.com>
Cc: linux-c-programming@vger.kernel.org
Subject: Re: Division in loop
Date: Tue, 8 Sep 2009 21:52:50 -0500	[thread overview]
Message-ID: <200909082152.50247.eric@cisu.net> (raw)
In-Reply-To: <34e1241d0909081939x2d20ab45t57fe3f760ae447d0@mail.gmail.com>

On Tuesday 08 September 2009 09:39:40 pm you wrote:
> Hi again...
>
> int fd; /* RS232 serial link */
> int fdp; /* file descriptor for a regular file */
> struct stat stat;
> size_t bytes = 0;
> void *buffer; /* buffer, will be allocated */
> ...
>    while (bytes < stat.st_size) {
>        ret = read(fdp, buffer + bytes, stat.st_size - bytes);
>        if (ret == -1) {
>            perror("read");
>            return -1;
>        }
>        ret = write(fd, buffer + bytes, ret);
>        if (ret == -1) {
>            perror("write");
>            return -1;
>        }
>        bytes += ret;
>        progress = (int) (bytes * 100) / stat.st_size;
>        printf("\rcompleted: %i%%", progress); /* NO OUTPUT, UNTIL LOOP
> ENDED */ fflush(stdout);
>    }
> ...
>
> Then i see printf() never output the message until the loop has ended...
> 'fdp' is a file descriptor to a local file, i send it through a serial
> connection (RS232),
> i use a serial connection so i can practice Linux's File I/O operation
> in real world ;p
>
> the receiver can output the progress's message, but the sender (the
> above code) could not...
> this is the receiver codes:
>
> ...
> 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 * 100) / size;
>        printf("\rcompleted: %i%%", progress); /* HAS OUTPUT */
>        fflush(stdout);
>    }
> ...
>
> On Tue, Sep 8, 2009 at 8:49 PM, Glynn Clements<glynn@gclements.plus.com> 
wrote:
> > Tim Walberg wrote:
> >> Maybe you want "(bytes * 100) / size"?
> >
> > However, if int and size_t are 32-bit, that will overflow at around
> > 20 or 40 MB (20 for signed, 40 for unsigned), which isn't really all
> > that much. Converting to either double or long long might be wise,
> > i.e.
> >
> >        percent = (int)(bytes * 100.0) / size;
> > or:
> >        percent = (int)(bytes * 100LL) / size;
> >
> > --
> > Glynn Clements <glynn@gclements.plus.com>
>
> --
> To unsubscribe from this list: send the line "unsubscribe
> linux-c-programming" in the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html

Your printf statements are being buffered by the system. If enough of them 
accumulate you will see them being printed "inside the loop". You need to 
disable  buffering or print to STDERR.

  reply	other threads:[~2009-09-09  2:52 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-09-08 14:40 Division in loop Randi Botse
2009-09-08 14:47 ` Saurabh Sehgal
2009-09-08 14:57 ` Bert Wesarg
2009-09-08 14:58 ` Tim Walberg
2009-09-08 15:09   ` Randi Botse
2009-09-09  0:49   ` Glynn Clements
2009-09-09  2:37     ` Randi Botse
2009-09-09  2:39       ` Randi Botse
2009-09-09  2:52         ` Eric Bambach [this message]
2009-09-09  2:57         ` Eric Bambach
2009-09-09 10:12           ` Randi Botse
2009-09-10  0:46         ` Glynn Clements
2009-09-10  7:01           ` Randi Botse
2009-09-10  7:03             ` Manish Katiyar
2009-09-11  7:26             ` Glynn Clements
2009-09-09  0:45 ` Zhenwen Xu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=200909082152.50247.eric@cisu.net \
    --to=bot403@cisu.net \
    --cc=eric@cisu.net \
    --cc=linux-c-programming@vger.kernel.org \
    --cc=nightdecoder@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).