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);
}
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.
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!
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.