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.
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;
}