Not sure what's happening w/ fstream.

As far as I know, when you invoke ifstream/fstream::open, specifically in ios::in mode, and the file you want doesn't exist, it's supposed to .fail(), right? That's what every reference I have is telling me, and yet it doesn't seem to be happening.

Here's the deal. I want my program to know whether or not a data file exists, and if it doesn't, it calls a different function to let the user create one. My solution was to call ifstream.open("foo.dat"), then check ifstream.fail() to find out if foo.dat was actually there. If fail is true, the file's not there; if fail is false, then it is there, correct? Apparently not.

Even if I deleted the file and then checked infile.fail(), it would just create the file and then tell me it existed! Is it just me, or should it not do that? I'm aware that if an ofstream attempts to open a nonexistent file, it will just create it, but I read in my college textbooks that an ifstream (in ios::in mode) that fails to find the file will not create one, and should in fact identify the failure by infile.fail() returning true! So, for whatever reason, that's not happening. Now, I tried adding the tag ios::nocreate, and that prevents infile.open from creating a new file, but I'm wondering why this method espoused by at least two textbooks and assorted websites isn't producing the expected results.

Finally, I shortened it to a program that just tests for it, and here it is in all its malfunctioning glory:

int main()
{
ifstream infile;
infile.open("foo.dat",ios::in);
if(infile.fail())
cout << "infile failure" << endl; // this will not appear
return 0;
}

In sum: why doesn't it work!?
Last edited on
If you do a validation on the fstream object it will return the state right away.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <fstream>
#include <iostream>

using namespace std;

int main()
{
    ifstream infile;
    infile.open("foo.dat",ios::in);
    if(!infile)
        cerr<<"infile failure"<<endl;
    return 0;
}
Good point, but it still doesn't explain why infile.open creates a new file. If there's no file from which to read input, it shouldn't create a blank one by default. Sure, I could just fix it with other code, but I'm curious as to why it isn't acting normally.
I checked both code, yours and the one I posted, it returned the error message on my machine. I did create a file and then I did not get the error message. I also went on and deleted the file and re-run the program once more, the error message was back.

I guess it might a problem with your machine or compiler.
Huh... based on your code, I tried inserting the "using" directive, and my compiler (Microsoft VC++ 6.0) flags it with this error:
" 'std' : does not exist or is not a namespace"
Now what's that about? Gah, maybe I should direct this question someplace else.
Hey, I got it working. Thanks for your help. Normally I use
#include <fstream.h>
-type syntax, but once I removed the ".h" it all came together and started behaving. Now, precisely how that irritating syntactical convention actually changes anything is beyond me... but it's another question I need answered.
Last edited on
I do not know what's inside the "fstream.h" that Microsoft is using so I can not exactly tell you what's the problem. I had a discussion before that not all libraries are the same across all platforms so that might be the reason. The standard "fstream" could've been different with the way Microsoft implemented their C++ standard libraries.

Then again, like I said, I can not tell this for sure cause I use the GNU compiler.
Topic archived. No new replies allowed.