Sign in to follow this  
wforl

eof character

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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


Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
but using your method ToohrVyk, could the flag have been any of the following

eofbit The end of the source of characters is reached during its operations.
failbit No characters were extracted because the end was prematurely found.Notice that some eofbit cases will also set failbit.
badbit An error other than the above happened.

where as using the above, it detects the eof flag?

Share this post


Link to post
Share on other sites
so getline() returns the istream object.


So by the statement


while (getline(objfile, fullLine))
{
...
}


will that return false if any of the flags are set? which is the same as objfile.good() i believe.

Im just trying to test for end of file, but i would also like a better under standing of the functions.

Share this post


Link to post
Share on other sites
Quote:
Original post by wforl
so getline() returns the istream object.


So by the statement


while (getline(objfile, fullLine))
{
...
}


will that return false if any of the flags are set? which is the same as objfile.good() i believe.


Yep. It's a standard idiom.

Quote:
Im just trying to test for end of file, but i would also like a better under standing of the functions.


And if the badbit or failbit get set instead, would you really like to do something different? :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this