2

This is a working snippet of a while loop:

while(total_bytes_read != fsize){
    while((nread = read(sockd, filebuffer, sizeof(filebuffer))) > 0){
        if(write(fd, filebuffer, nread) < 0){
            perror("write");
            close(sockd);
            exit(1);
        }
        total_bytes_read += nread;
        if(total_bytes_read == fsize) break;
    }
}

This is an example of a NON working snippet of a while loop:

while(total_bytes_read != fsize){
    while((nread = read(sockd, filebuffer, sizeof(filebuffer))) > 0){
        if(write(fd, filebuffer, nread) < 0){
            perror("write");
            close(sockd);
            exit(1);
        }
        total_bytes_read += nread;
    }
}

And also this, is an example of a NON working snippet of a while loop:

while(total_bytes_read < fsize){
    while((nread = read(sockd, filebuffer, sizeof(filebuffer))) > 0){
        if(write(fd, filebuffer, nread) < 0){
            perror("write");
            close(sockd);
            exit(1);
        }
    }
     total_bytes_read += nread;
}

I would like to know why into the 2 snippet above when total_bytes_read is equal to fsize the loop won't exit :O
Thanks in advance!

3
  • if you're positive that total_bytes_read == fsize then you are probably being caught in the second while loop because this: (nread = read(sockd, filebuffer, sizeof(filebuffer))) > 0 evaluates to true Commented Jul 3, 2012 at 13:53
  • Also note that read(2) from sockets doesn't return 0 unless the other party has issued closed the connection in a proper way. Commented Jul 3, 2012 at 14:57
  • what do you mean by "proper way"?? Commented Jul 3, 2012 at 15:10

3 Answers 3

6

The outer loop in snippets 2 and 3 will not exit because the inner loop does not exit: the total_bytes_read != fsize loop never gets a chance to check its continuation condition.

Snippet 1 works fine, because you check the same condition inside the nested loop, and break out if the limiting count has been reached.

You can combine both conditions into a single loop, like this:

while((total_bytes_read != fsize) && (nread = read(sockd, filebuffer, sizeof(filebuffer))) > 0) {
    ...
}
Sign up to request clarification or add additional context in comments.

Comments

0

Because you removed the break that stops the loop

if(total_bytes_read == fsize) break;

1 Comment

yes i know this :) my question was about "why without it the while loop won't exit even it has the same condition" :)
0

There's a reason not to code read loops this way. (Actually, there are many.) If you compute fsize and then the file changes size while you are reading, your loop will never terminate. Don't do this! There is no need to compute the file size; just read data until there is no more.

6 Comments

But i've read that "feof" is bad into while loop (faq.cprogramming.com/cgi-bin/…)
Using feof in the while condition is also not appropriate. Read until read returns <= 0. Then stop reading.
but why read until it returns <= 0?? the return status of read is the number of bytes if success!
Because read returns 0 to indicate that you have read the entire file.
but why "less or equal than 0" instead of "greater or equal than 0"?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.