eof character

Started by
12 comments, last by Zahlman 15 years, 9 months ago
Im reading in some text files that i make in general editors, but when using the standard !eof() function i find that the files being read only get detected for this when made by certain apps. For example when i export from one app, the end of file gets detected and all is well. But when i export from this other partcicular app, the end of file bit doesn't get dtetced and it just runs over into ram. Can somone tell me if there is an application/text editor i can use to load in the particular files, to check if there is indeed an eof byte in the actual file. And also what the byte is? i believe its a -1?
Advertisement
There is no 'EOF byte' -- feof() just returns a nonzero value if the file pointer in question has the end-of-file indicator set. C and C++ have an EOF macro, but this just a system-defined value that is used to indicate that the EOF condition has occured, it's not a value you get from the file itself.
There's no such thing as an "EOF byte", at least one which is actually recognized as an EOF by the feof() function. Your interpretation of the situation is incorrect. Much more likely is that you weren't aware that the feof() function does not return true until after a read has failed. In other words, feof() returning false does not mean that there's more data to read, only that no read has failed yet.
Quote:Original post by Sneftel
There's no such thing as an "EOF byte", at least one which is actually recognized as an EOF by the feof() function. Your interpretation of the situation is incorrect. Much more likely is that you weren't aware that the feof() function does not return true until after a read has failed. In other words, feof() returning false does not mean that there's more data to read, only that no read has yet failed for that reason.


Minor fix. :)

There exists a variant of the 'get' function which reads one character, but reads it as an int, returning 0 through 255 if there is an available character, and -1 if the file is at the end. This might be what you were thinking of.

Show the code that you use to read the files.
This is covered in the C++ FAQ lite which every C++ programmer should do themselves a favor and take some time to look over:

"
[15.5] Why does my input seem to process past the end of file?

Because the eof state may not get set until after a read is attempted past the end of file. That is, reading the last byte from a file might not set the eof state. E.g., suppose the input stream is mapped to a keyboard — in that case it's not even theoretically possible for the C++ library to predict whether or not the character that the user just typed will be the last character.
"

What you are trying to do is unreliable as you have found out.
[size="2"]Don't talk about writing games, don't write design docs, don't spend your time on web boards. Sit in your house write 20 games when you complete them you will either want to do it the rest of your life or not * Andre Lamothe
Some systems used a real character (^Z == 0x26) for EOF. I haven't seen that used in a LONG time, though.

Most file APIs I've used lately check for EOFs by comparing the current position in the file to the length.
well here is mt code

std::string fullLine;	std::string firstword;		while(!objFile.eof())	{		count++;		getline(objFile, fullLine);		std::stringstream line(fullLine);		line >> firstword;         //process firstline
Quote:Original post by maya18222
getline(objFile, fullLine);


After you execute this line, you should check whether you reached the end-of-file. If you did, then no line was read and the original contents of the string are still there. See daviangel's post for more information.
somrthing like this?

while(objFile)	{		getline(objFile, fullLine);		if(objFile.eof())			break;
while (getline(objfile, fullLine)){  ...}

This topic is closed to new replies.

Advertisement