www.delorie.com/djgpp/bugs/show.cgi   search  
Bug 000147

When Created: 03/31/1997 14:23:13
Against DJGPP version: 2.01
By whom: shimmer@infinet.com
Abstract: Problems opening files in binary mode
In version 2.01, I can't get it to open a file correctly in binary mode, 
using ifstream file(&name, ios::binary) or ofstream file(&name, ios::binary)
Files opened with ifstream are returned incorrectly, and files written with 
ofstream are filled with the null character (0x00).  The code runs fine
under Borland Turbo C++ 3.1 for Windows.  (The program's written for DOS, of
course. ;)

If you want to see the code, e-mail me.

Workaround added: 08/28/1997 20:04:34
By whom: pjfarley@dorsai.org
Adam Lee had a similar problem reported in the newsgroup.  This is a workaround 
based on his problem.

One must include the expression "ios::in | ..." or "ios::out | " along with the
"ios::binary" specification.  I don't know if the open method is supposed to
"or" the default "mode" (which is ios::in or ios::out, respectively, for 
ifstreams and ofstreams) with the specified mode(s), but it is readily apparant
that it does not do so; leave out the ios::in or ios::out and it will not work.

Working DJGPP example follows:

#include <fstream.h>

int main(int argc, char *argv[])
{
    char temp[1025];
    if(argc<3)
    {
        cout << "Usage: " << argv[0] << " <infile> <outfile>" << endl;
        return 1;
    }
    ifstream in(argv[1], ios::in | ios::binary);
    if(in.fail())
    {
        cout << "Error opening " << argv[1] << endl;
        return 2;
    }
    ofstream out(argv[2], ios::out | ios::binary);
    if(out.fail())
    {
        cout << "Error opening " << argv[2] << endl;
        return 3;
    }
    while (!in.eof()&&!in.fail()&&!out.fail())
    {
    {
        in.read(temp,1024);
        cout << "Length read was " << in.gcount() << endl;
        cout << "in.eof=" << in.eof() << ", in.fail=" << in.fail() <<
            ", out.fail=" << out.fail() << endl;
        out.write(temp, in.gcount());
    }
    out.flush();
    if(in.eof())
        cout << "End of file reached." << endl;
    if(in.fail()&&!in.eof())
        cout << "Error reading " << argv[1] << "." << endl;
    if(out.fail())
        cout << "Error writing " << argv[2] << "." << endl;
    in.close();
    out.close();
    return 0;
}

Note added: 08/28/1997 20:06:47
By whom: pjfarley@dorsai.org
OOPS!  I pasted some bad code in the workaround I posted:

    while (!in.eof()&&!in.fail()&&!out.fail())
    {
    {

should really be

    while (!in.eof()&&!in.fail()&&!out.fail())
    {

Sorry about that.

Closed on 04/12/1999 11:00:03: The problem is in the ported C++ library, not in DJGPP.
By whom: eliz@is.elta.co.il



  webmaster     delorie software   privacy  
  Copyright © 2010   by DJ Delorie     Updated Jul 2010