This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

libstdc++ [io]fstream cannot open file in binary mode (g++ and egcs)


Hi.

I run into the following problem with two compilers
on two different platforms:
1) gpp 2.95.1 on DOS (DJGPP port, on a plain i386/DOS 6.22 machine) and
2) g++ on Linux/i386 (one ships with RedHat 6.1 Linux: egcs-2.91.66)

I can not open fstream in BINARY mode,
e.g. I got zero size file from the sample program below.


#include <iostream>
#include <fstream>

int main (int argc, char *argv[])
{
        ofstream fpw (argv[1], ios::binary);

        for (int i=0; i<256; i++) {
                fpw.put( ((char)i) );
        }
}


Without "ios::binary" it is OK, but file opened in text
mode which have a difference on a DOS machine, sure.
Info page says one should be ios::bin which doesn't work either; and
Strausrup' "The C++ Programming Language, 3rd ed." says it should be
"ios_base::binary" which will not compile on g++ at all.


After some experimentation I found out that
        ofstream fpw (argv[1], ios::binary | ios::out);
will work properly, but seems excessive when ofstream declared
explicitely.

The same thing with
        ifstream fp (somename, ios::binary);
vis
        ifstream fp (somename, ios::binary | ios::in);


Seems heavily undocumented behaviour.

Sincerely,

Leonid.






Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]