[Bug libstdc++/12409] cannot reuse ofstream due to interaction between <string> and <fstream>

kouptsov at wsu dot edu gcc-bugzilla@gcc.gnu.org
Fri Sep 26 00:44:00 GMT 2003


PLEASE REPLY TO gcc-bugzilla@gcc.gnu.org ONLY, *NOT* gcc-bugs@gcc.gnu.org.

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=12409



------- Additional Comments From kouptsov at wsu dot edu  2003-09-26 00:16 -------
There are actually two bugs related to <fstream>:

1. The file is opened but os.is_open() gives 'false':

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

int main () {

  string filename;
  ofstream os;

  filename = "data-0";
  os.open(filename.c_str());
  for(int i=-5; i<5; i++) os<<i<<" "<<endl;
  os.close();
  
  filename = "data-1";
  os.open(filename.c_str());
  if(os.is_open()) {cerr<<"file '"<<filename<<"' not opened"<<endl;} /* exit(-1)
removed */
  
  for(int i=-5; i<5; i++) os<<i<<" "<<endl;
  os.close();
  
  return 0;
}

$ make
g++    -c -o openfile-2.o openfile-2.cpp
g++ -Wall -lm -o run openfile-2.o 

$ run
file 'data-1' not opened

$ ls -l data-*
-rw-r--r--    1 klk206   users          35 Sep 25 17:12 data-0
-rw-r--r--    1 klk206   users          35 Sep 25 17:12 data-1

The file data-1 was opened and data written.


2. Reported in #12412.



More information about the Gcc-bugs mailing list