New problem with fstreams?!?

Paolo Carlini pcarlini@unitus.it
Sat Jan 26 04:05:00 GMT 2002


Hi all, hi Benjamin,

my friend Charles Leggett brought to my attention a recent problem with
fstreams. This a reduced
testcase:

//////////
#include <iostream>
#include <fstream>

std::fstream scratch_file;

int main()
{
  scratch_file.open("SCRATCH", std::ios::out);
  scratch_file << " just stuff " << "\n";
  scratch_file.close();

  for (int i=0; i<10; ++i)
    {
      std::cout << i << "\n";

      scratch_file.open("SCRATCH", std::ios::in);
      if(!scratch_file) {
        std::cerr << "Cannot open SCRATCH\n";
        abort();
      }
      scratch_file.close();
    }
}
//////////

Currently, this is the output:
0
1
Cannot open SCRATCH
Abort

On the other hand, if I revert this patch:

2002-01-08  Benjamin Kosnik  <bkoz@redhat.com>

        libstdc++/2913
        libstdc++/4879
        * include/bits/fstream.tcc (filebuf::_M_really_overflow): Test
        return value of _M_file->sync().
        (filebuf::showmanyc): Check for is_open.
        * include/std/fstream (filebuf::sync): Tweak.

the behavior is completely different (the same delivered by 3.0.3):
0
1
2
3
4
5
6
7
8
9

Does this trigger you something?

Cheers,
Paolo.




More information about the Libstdc++ mailing list