Mail Archives: djgpp/1997/08/28/05:33:52
adalee AT sendit DOT sendit DOT nodak DOT edu (Adam W Lee) wrote:
>Adam W Lee (adalee AT sendit DOT sendit DOT nodak DOT edu) wrote:
>: I'm trying to write a program to read a file and write it to another file
>: in C++, but my program quits like halfway through the file with a read
>: error or something to that effect... Here's some pseudo-code.
Adam,
I took your example and tried it myself, and yes, it fails as you say.
After a little experimentation, I found a version that works perfectly
under DJGPP. It seems that the "ios::in" and "ios::out" are
*required* in the open process, even though the files are declared
ifstream and ofstream, respectively. Please try my version to
confirm.
#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;
// in.open(argv[1], ios::binary); <=== THIS FAILS
in.open(argv[1], ios::in | ios::binary); // THIS WORKS
if(in.fail())
{
cout << "Error opening " << argv[1] << endl;
return 2;
}
ofstream out;
// out.open(argv[2], ios::binary); <=== THIS FAILS
out.open(argv[2], ios::out | ios::binary); // THIS WORKS
if(out.fail())
{
cout << "Error opening " << argv[2] << endl;
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;
}
----------------------------------------------------
Peter J. Farley III (pjfarley AT dorsai DOT org)
- Raw text -