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.

Cheers

#include<fstream>
#include<iostream>
#include<cstdlib>
using namespace std;

int main()
{

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


ifstream iFrog;
ofstream oFrog;

iFrog.open("iFrogFile.txt");
if (iFrog.fail())
{
cout<<"Input file opening failed"<<endl;
exit(1);
}

oFrog.open("oFrogFile.txt");
if (oFrog.fail())
{
cout<<"Output file opening failed"<<endl;
exit(1);
}

oFrog<<123<<231<<742;
iFrog>>test1>>test2>>test3;

cout<<test1<<endl<<endl<<test2<<endl<<endl<<test3<<endl<<endl;

iFrog.close();
oFrog.close();

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.

Users/AppData/Roaming/Microsoft/Windows/Recent/

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.