Now that the code has been moved from using the old style .h header files to the new style header files, a simple method of using fstream object to open an existing file now fails.
The fstream is for both reading and writing and this exact same code previously worked with the old style headers. Note this same behavior results from both MS 6.0 as well as 7.1 compilers.
MSDN indicates opening in ios::app mode performs seek to the end of the file and new bytes are always written to the end even if position moved via seekp function. However, we are not even getting this far for the file fails to open (note that the file does exist and has read-write attributes for current user).
Can anyone explain what is wrong with use of fstream in the following example when opening to write to a file?
#include <fstream>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
fstream firstFile("c:\\t1.log", ios::app);
ofstream secFile("C:\\t2.log", ios::app);
if ( firstFile.is_open() == 0 )
cout << "Failed to open t1.log" << endl;
else
{
firstFile << "file opened with fstream" << endl;
firstFile.close();
}
if ( secFile.is_open() == 0 )
cout << "Failed to open t2.log" << endl;
else
{
secFile << "file opened with ofstream" <<endl;
secFile.close();
}
return 0;
}
The fstream is for both reading and writing and this exact same code previously worked with the old style headers. Note this same behavior results from both MS 6.0 as well as 7.1 compilers.
MSDN indicates opening in ios::app mode performs seek to the end of the file and new bytes are always written to the end even if position moved via seekp function. However, we are not even getting this far for the file fails to open (note that the file does exist and has read-write attributes for current user).
Can anyone explain what is wrong with use of fstream in the following example when opening to write to a file?
#include <fstream>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
fstream firstFile("c:\\t1.log", ios::app);
ofstream secFile("C:\\t2.log", ios::app);
if ( firstFile.is_open() == 0 )
cout << "Failed to open t1.log" << endl;
else
{
firstFile << "file opened with fstream" << endl;
firstFile.close();
}
if ( secFile.is_open() == 0 )
cout << "Failed to open t2.log" << endl;
else
{
secFile << "file opened with ofstream" <<endl;
secFile.close();
}
return 0;
}