which of these codes is better?
do {
fread( ... );
if(!feof(fp)) { ... }
}
while(!feof(fp));
or
while(1){
fread( ... );
if(!feof(fp)) { ... }
else break;
}
Thanks.
which of these codes is better?
do {
fread( ... );
if(!feof(fp)) { ... }
}
while(!feof(fp));
or
while(1){
fread( ... );
if(!feof(fp)) { ... }
else break;
}
Thanks.
Neither. You are better off making the eof test part of the loop condition (at the top).
You can do this:
while (!feof(fp)) {
fread(...);
}
Since fread
returns the number of objects read, you could should also do it this way:
while (fread(...) != 0) {
}
The while
loop is better since the do while
do the same operations but it's calling the feof()
function twice.
which is better?
No one is better than another. The only difference in between these two that first one iterate at least once.