主题:[讨论]为何这个程序无法工作
各位 这个程序为何无法完成工作 是一个文件拷贝程序
#include<cstdlib>//for the definition of EXIT_FAILURE
#include<fstream>
#include<iostream>
using namespace std;
#define infile "indata.txt"
#define outfile "outdata.txt"
int copyline(ifstream&,ofstream&);
int main()
{
int linecount;
ifstream ins;
ofstream outs;
ins.open(infile);
if(ins.fail())
{
cerr<<"ERROR:coanot open file"<<infile<<endl;
return EXIT_FAILURE;
}
outs.open(outfile);
if(outs.fail())
{
cerr<<"ERROR:cannot open file"<<outfile<<endl;
return EXIT_FAILURE;
}
linecount=0;
while(!ins.eof())
{
if(copyline(ins,outs)!=0)
linecount++;
}
cout<<"input file copied to output file"<<endl;
cout<<linecount<<"lines copied"<<endl;
ins.close();
outs.close();
return 0;
}//main end
int copyline(ifstream& ins,ofstream& outs)
{
const char NWLN='\n';
int charcount=0;
char nextch;
ins.get(nextch);
while((nextch!=NWLN)&&!ins.eof())
{
outs.put(nextch);
charcount++;
}
if(!ins.eof())
{
outs.put(NWLN);
charcount++;
}
return charcount;
}
#include<cstdlib>//for the definition of EXIT_FAILURE
#include<fstream>
#include<iostream>
using namespace std;
#define infile "indata.txt"
#define outfile "outdata.txt"
int copyline(ifstream&,ofstream&);
int main()
{
int linecount;
ifstream ins;
ofstream outs;
ins.open(infile);
if(ins.fail())
{
cerr<<"ERROR:coanot open file"<<infile<<endl;
return EXIT_FAILURE;
}
outs.open(outfile);
if(outs.fail())
{
cerr<<"ERROR:cannot open file"<<outfile<<endl;
return EXIT_FAILURE;
}
linecount=0;
while(!ins.eof())
{
if(copyline(ins,outs)!=0)
linecount++;
}
cout<<"input file copied to output file"<<endl;
cout<<linecount<<"lines copied"<<endl;
ins.close();
outs.close();
return 0;
}//main end
int copyline(ifstream& ins,ofstream& outs)
{
const char NWLN='\n';
int charcount=0;
char nextch;
ins.get(nextch);
while((nextch!=NWLN)&&!ins.eof())
{
outs.put(nextch);
charcount++;
}
if(!ins.eof())
{
outs.put(NWLN);
charcount++;
}
return charcount;
}