Hello guys!
I'm facing a problem with IO using files.
Here is the simple code that's kepping me awake:
Files::Files()
{
numbers.open("textfile.txt");
if ( numbers.good() )
{
cout << "Object from Files class ok!" << endl;
}
else
{
cout << "Object from Files class NOT ok!" << endl;
}
}
'numbers' is a fstream object. According to MSDN, there's no default open mode for fstream, but some of the various 'open' definitions, the default is in and out. Confused.
That's my class constructor. The Files class only writes and reads numbers from the file above.
My problem lies here: numbers.open("textfile.txt");
, this method doesn't create the file if it doesn't exist. Already tried with numbers.open("textfile.txt", ios::in | ios::out);
and still no luck.
I can't use ios::trunc or ios::app since is a must to write anywhere in file and don't lose it's content. And I can't manually create the file since the user can select different files to save/load at runtime.
I'm using Visual Studio 2008 on Windows 7 x64. Since that code is so simple, I'm starting to think it's a VS2008 problem or such;
Please advise!
ps: Taken from MSDN:
"The member function opens the file with filename filename, by calling fopen(filename, strmode). strmode is determined from mode &~(ate & | binary):
-
ios_base::in
becomes "r" (open existing file for reading). -
ios_base::out or ios_base::out | ios_base::trunc
becomes "w" (truncate existing file or create for writing). -
ios_base::out | app
becomes "a" (open existing file for appending all writes). -
ios_base::in | ios_base::out
becomes "r+" (open existing file for reading and writing). -
ios_base::in | ios_base::out | ios_base::trunc
becomes "w+" (truncate existing file or create for reading and writing). -
ios_base::in | ios_base::out | ios_base::app
becomes "a+" (open existing file for reading and for appending all writes)."
"
So ios_base::in | ios_base::out
should work, but... no... snif...