bug ofstream and ios::trunc

peeterj@ca.ibm.com peeterj@ca.ibm.com
Tue Sep 26 21:04:00 GMT 2000


Hi,

Using both gcc-2.95.2/lib/gcc-lib/powerpc-ibm-aix4.3.2.0 and
i386-redhat-linux/egcs-2.91.66 I am seeing that when ios::trunc is
specified in the ofstream constructor mode parameter, no output is ever
generated, as the following small samples illustrate.

/* no_output.cc */
#include <fstream.h>
int main()
{
        ofstream f("blah", ios::trunc);
        f << "hi\n";
        return 0;
}

vs.

/* gives_output.cc */
#include <fstream.h>
int main()
{
        ofstream f("blah");
        f << "hi\n";
        return 0;
}

It seems that ios::out is not added to the ofstream mode flags if trunc
(and presumably other flags) are used for the mode.  This doesn't make much
sense for an ofstream (out by definition), but I am not sure if the
standard says anything about whether this is bad behaviour or not
(emperically, the C++ compilers on at least AIX, Sun, Dynix, HP and NT all
have ofstreams that set ios::out in addition to whatever flags are
specified by the user.)

Peeter



More information about the Libstdc++ mailing list