Hi, when I read entries in file i.e text file, how can I determine the first line and the last line ? I know the first line of entry can be filtered using counter, but how about the last line of entry in EOF while loop ? while (! file.eof() ) { ....
You should not ever write: while (! file.eof() ) { ... } Suppose you have a file with 10 characters, and you write: while (! eof(file)) { fread(buf, 1, 10, file); ...} You will go through the loop twice--on the first loop, buf will contain the contents of the file after fread returns. (Assuming no errors.) When you return to the top of the loop, eof() will return 0. You then call fread, which will return 0, and buf still contains the contents of the file and the loop is processed. Then eof() returns true. ie, eof() doesn't return true until you've actually read an eof, which doesn't happen until you attempt to read more data than is available. You should construct your loop as while ( fread(buf, sizeof *buf, 1, file) == 1) { ... }
I got the answer from other forum but I am definitely looking forward to this exiting forum and have lots of questions as I am starting off with the programming.