Mail Archives: djgpp/1997/08/28/04:50:39
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.
OK, now the plot thickens :) This program works perfectly under
BSDi/OS... Here's the actual code:
#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]);
if(in.fail())
{
cout << "Error opening " << argv[1] << endl;
return 2;
}
ofstream out(argv[2]);
if(out.fail())
{
cout << "Error opening " << argv[2] << endl;
return 3;
}
while (!in.eof()&&!in.fail()&&!out.fail())
{
in.read(&temp,1024);
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;
}
- Raw text -