Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

EOF in C++

Hi!

I have a problem with eof() function in C++. I have such file -

http://theval.eclub.lv/outCBC.txt as input file and I want to read all the characters from that file and put them on screen. I write it this way:

while (!infile.eof()) {
infile.get(c);
cout <<c;
}

But the problem is the character which is 8th from the end - the one right after the character `C` - C++ can't read the file further from this symbol. So it reads this `C` character and then it thinks it is the end of file already. But why?? How must I change the code to read all the file?

Edgars.

P.S. I think the ASCII code of this 8th character from the end is 26.
[717 byte] By [TheVAL] at [2007-11-11 10:13:28]
# 1 Re: EOF in C++
Open the file in binary mode
and then read.
it career at 2007-11-11 20:59:18 >
# 2 Re: EOF in C++
even opening in binary can trip it under some compilers. Its safer to read the file size and loop over that many bytes or better still read the whole file into a buffer at one go (fast!) and grab next buffer item each iteration.
jonnin at 2007-11-11 21:00:14 >
# 3 Re: EOF in C++
I tried to use this buffer method, but nothing changed. But it looks like it works for now if using this binary opening method! Thank you!
TheVAL at 2007-11-11 21:01:12 >
# 4 Re: EOF in C++
it's certainly the ios::binary opening mode.
Danny at 2007-11-11 21:02:23 >
# 5 Re: EOF in C++
guess thats a legacy of visual 6 in my head, that binary mode didnt seem to help in every case. I still occasionally do things oddly because of the years I spent under that compiler.
jonnin at 2007-11-11 21:03:22 >
# 6 Re: EOF in C++
I guess jonnin u mean a special case in some files, when their structure has two knowen restricted chars, one for defining the line end and one for fileend (like EOT in the bootstrab read section) , if u use to open one of those files in notepad , the lines aren't separeted with a return but a special char and u see all the lines are consecutive without any separations between them (as notepad can't display this special char) , and if u write to them when u don't know this restristed u may print one of them , it may be this endoffile and then whenever u read it by binary or any thing u will have this char as flag for the file end and can't continue reading .
but when u force it to read a special storage size , it hasen't any choice and it will continue the read process .
Amahdy at 2007-11-11 21:04:16 >