[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
<fstream> close ( ) bug
From: |
Paul Langner |
Subject: |
<fstream> close ( ) bug |
Date: |
Thu, 22 Jul 2004 11:29:13 -0500 |
Below is a very simple program that illustrates the problem. It consists of
two files, an ifstream and an ofstream. First the program opens file1,
writes something to it, and reads it back, closing the file between
operations. Then the same streams are used to do the same things to a
second file. On trying to open the second file for reading, the stream
dies, indicating Fail and EOF.
This program fails under g++ and Microsoft Visual C++ .NET, but it worked
under the old fstream.h libraries, and several older Borland compilers. I
can "fix" the problem by inserting source.clear ( ) after the open, but this
is dubious.
Paul Langner
#include <iostream>
#include <fstream>
using namespace std;
int main ( )
{
//declare local variables
char* file1 = "test1.dat";
char* file2 = "test2.dat";
char c;
ofstream target;
ifstream source;
//open file1 and write something and then close the stream
target.open (file1);
target << "Adding a line to file 1\n";
target.close ( );
//open file1 and read it back
source.open (file1);
while (source.get (c)) cout << c;
source.close ( );
//open file2 and write something
target.open (file2);
target << "Adding a line to file 2\n";
target.close ( );
//program will fail at this point
cout << "At point of failure" << endl;
source.open (file2);
if (!source)
{
cout << "Error opening " << file2 << ": " << source.rdstate ( ) << endl;
return 0;
}
while (source.get (c)) cout << c;
source.close ( );
return 1;
}
<<attachment: winmail.dat>>
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- <fstream> close ( ) bug,
Paul Langner <=