Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why does open file fail with use of fstream?

Status
Not open for further replies.

Muir27

Programmer
Nov 1, 2005
2
US
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;
}

 
You should be using ios_base::app, not ios::app. See if that fixes things.
 
Thanks for the reply uolj but changing the mode to ios_base::app does not fix the problem. The fstream object continues to fail to open the existing file.
 
I had some strange problems with fstream until I put Visual Studio Service Pack 5 in. I don't know if it is relavant to this situation but I thought I would mention it.

If you choose to battle wits with the witless be prepared to lose.

[cheers]
 
You need to specify ios_base::eek:ut when using fstream for VC 7.1 (I assume it is the same for 6.0 as well).
Code:
#include <fstream>
#include <iostream>
using namespace std;

int main(int argc, char *argv[])
{
  fstream  firstFile("c:\\t1.log", ios_base::out | ios_base::app);
  ofstream  secFile("C:\\t2.log", ios_base::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;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top