Archived

This topic is now archived and is closed to further replies.

ironfroggy

ifstream errors

Recommended Posts

Guest Anonymous Poster
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.

Share this post


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

Share this post


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

Share this post


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

Share this post


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

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.

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
ok ive got the file to open correctly now it seems
but it still wont read the file. i am using ifstream::get() to put the data into ch (iFile.get(ch)) but ch doesnt seem to get a new value after i call get()

Share this post


Link to post
Share on other sites
quote:
Original post by ironfroggy
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.

Trunc will clear the file when you open it, cos that''s what it''s there for. So you''ll never be able to read anything from it if you keep removing its contents beforehand. You don''t want trunc when you just want to read. By the way, trunc shouldn''t work without the out flag too, so perhaps you''re setting it or something.

Share this post


Link to post
Share on other sites