From lucia@site.uottawa.ca Fri Feb 3 15:55:43 2006 Date: Fri, 3 Feb 2006 15:55:02 -0500 (EST) From: Lucia Moura To: undisclosed-recipients: ; Subject: End of file detection Dear csi2131 students: The following exaplanation may help with some problems you might have reading files. This is useful for this and future assignments. In particular, some people had problems reading lowcomp1.txt, in that they process the last line twice. What comes below might help in understanding/fixing that. First we will look at reading a file character by character. 1) When we read the last character in a file,the eof() flag is not ON yet. It is only when we try to read beyond the last character that it turns on. For this reason, the piece of code below is not correct: char c; while (!infile.eof()) { infile.get(c); cout<>num1>>num2;" where num1,num2 are long ints, end-of-file is not detected after reading the last line. The folllowing piece of code won't work in that it will loop 4 times for a file with 3 lines (it will process the last line twice): ------------- wrong ------------------------ while (!infile.eof()) { infile >> num1 >> num2; } -------------------------------------------- You need to correct it in the same way as the example above in 1). Lucia Moura