ifstream errors

Started by
11 comments, last by ironfroggy 22 years, 8 months ago
it reads an EOF as the first thing. The file tho does have content. Its an html file.
(http://www.ironfroggy.com/)(http://www.ironfroggy.com/pinch)
Advertisement
A friend of mine had this problem before. He had it on a Solaris machine and it wound up that whenever he opened an ifstream the file pointer pointed to the end of the file. He had to reset the file pointer by making a call to seekg, or some function with roughly the same name.
How do you know it is reading an EOF? What functions are you using? Post a little code. Chances are, you are either performing some sort of read that is too long, or you are simply failing to open the file in the first place. Check the successful opening of the file with the ! operator before trying to read anything.

Edited by - Kylotan on August 11, 2001 5:07:08 AM
I open the file with ifstream::open() and check for errors with ifstream::fail()
I read a single char with the operator>> method (iFile >> ch

I have done some debugging tho and now I see that it isnt reading that value, it isn''t reading anything. The char doesn''t get any value at all from iFile.
(http://www.ironfroggy.com/)(http://www.ironfroggy.com/pinch)
Check the file like this after you open it and before you read anything:

if (!file){    // error occurred}else{    // Read in data} 


This usually means file not found, and lets you distinguish between not-found files, and empty/corrupt files.
I might have misunderstood your answer Kylotan, but checking whether file (an instance of ifstream) is "true" or not NULL won''t work.

My guess would be than the file name is wrong. Remember than you''ve got to use double backslashes to specify Windows pathes. So c:\windows becomes c:\\windows (because \ is the escape character). Or use the Posix path (Windows has a Posix subsystem) like c:/windows.

From MSDN :
To read a file, first use the fail member function to determine whether it exists:

istream ifile( "FILENAME", ios::nocreate );
if ( ifile.fail() )
// The file does not exist ...

However, I''m not sure than ios::nocreate is Ansi.
quote:Original post by Anonymous Poster
I might have misunderstood your answer Kylotan, but checking whether file (an instance of ifstream) is "true" or not NULL won''t work.

I''d love to quote from the standard here, but I''m too cheap to buy a copy for myself. So I''ll just quote from MSDN:

basic_ios::operator!
bool operator!() const;
The operator returns fail().

basic_ios::operator void *
operator void *() const;
The operator returns a null pointer only if fail().

The ! and void* operators on a stream are for exactly this purpose. Yes, you can use file.fail() too, but it''s nice to use one of the two above operators (a) for brevity, and (b) to maintain syntactic equality with legacy C code.

The main point I was trying to make to Ironfroggy though, is that you check this before you read in your first item. Otherwise you can''t tell whether you have a missing file, an empty file, or a file that simply contained a value incompatible with the one you tried to read.

Lastly, nocreate is non-standard, and not necessary if you''re using standard iostreams. Istreams define the ''in'' flag by default and ios_base::in means "open existing file for reading".
I got the code from a friend, he said for text files used the ios::trunc argument. Also, the file is cleared after the program runs.
(http://www.ironfroggy.com/)(http://www.ironfroggy.com/pinch)
I just fixed some things, but only enough to get a better idea, sort of, of what the error is.

it says "Debug Assertion Failed" in fopen.c, which isnt in my project, at line 54, expression *file != _T(''\0'')
(http://www.ironfroggy.com/)(http://www.ironfroggy.com/pinch)
Did you use the same ifstream to open another file before?

This topic is closed to new replies.

Advertisement