File I/O - I'm Stuck

I'm trying to get a handle on basic file i/o and am totally stuck. This is the code - it complies with no errors or warnings but fails when run with the input file .open fail message. I'm using visual studio 2010 on a Vista laptop. I've tried using the full path of a specially set up directory C:/C++Files but that doesn't work.

I guess this is an old favorite but a search did not help. Any assistance much appreciated.


using namespace std;

int main()

int test1 =0, test2 =0, test3 =0;

ifstream iFrog;
ofstream oFrog;"iFrogFile.txt");
if (
cout<<"Input file opening failed"<<endl;
if (
cout<<"Output file opening failed"<<endl;




return 0;
Are you able to open the output file? Try commenting out the exit(1) call in the input file open test to see. If it does open is the input file in the same location as the output file you created?

Also you should try to avoid using the exit() function is C++ programs. This function doesn't know how to properly use C++ classes and can cause problems.
The output file does open.

It's in a weird location.


No sign of the input file in that directory or any other - I've searched the name.

edit: The output file is also in the Visual Studio Project Directory - same location as the .cpp file.
Last edited on
Are you saying that the input file doesn't exist, not just for this program, but simply doesn't exist at all?
Chervil - Yes nothing comes up on a full Windows search.

Now I've found that after manually saving an empty text file (from notepad) called iFrogFile to the same directory as the oFrogFile the program runs fine. I can put data into iFrogFile and get it out.

So why couldn't Visual Studio create the iFrogFile but managed oFrogFile no problem? Weird!

Not weird. A program can only create output files.
ifstream is an input-only file, the program won't create it.
Last edited on
An ifstream by default won't open a non-existing file, but if you use the std::ios::app flag it will create the file if it doesn't exist. I do agree that creating an empty file for reading isn't very useful but it can be done.
Thanks for the help - sorted :-)
Topic archived. No new replies allowed.