Jump to content
  • Advertisement

Archived

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

ironfroggy

ifstream errors

This topic is 6184 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
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 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.

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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!