我在书上看到一段关于文件复制的代码:#include<iostream>
#include<errno>
using namespace std;
int main(int argc,char * argv[])
{
FILE *in_file, *out_file;
char rec[256];
size_t bytes_in, bytes_out; if(argc!=3)
{
cout<<"Usage: cpc file1 file2\n";
}
return 1; in_file=fopen(arfv[1],"rb");
if(in_file=NULL)
{
// perror(argv[1]);
}
return 2; out_file=fopen(argv[2],"wb");
if(out_file=NULL)
{
//perror(argv[2]);
}
return 3; while((bytes_in=fread(rec,1,256,in_file))>0)
{
bytes_out=fwrite(rec,1,bytes_in,out_file);
if(bytes_in!=bytes_out)
{
// perror("Fatal write error");
}
return 4;
} fclose(in_file);
fclose(out_file);
return 0;
}
编译链接后没有错误,假如我在f盘有两个txt文件:1.txt,2.txt,如何通过dos窗口将1中内容复制到2中呢?
#include<errno>
using namespace std;
int main(int argc,char * argv[])
{
FILE *in_file, *out_file;
char rec[256];
size_t bytes_in, bytes_out; if(argc!=3)
{
cout<<"Usage: cpc file1 file2\n";
}
return 1; in_file=fopen(arfv[1],"rb");
if(in_file=NULL)
{
// perror(argv[1]);
}
return 2; out_file=fopen(argv[2],"wb");
if(out_file=NULL)
{
//perror(argv[2]);
}
return 3; while((bytes_in=fread(rec,1,256,in_file))>0)
{
bytes_out=fwrite(rec,1,bytes_in,out_file);
if(bytes_in!=bytes_out)
{
// perror("Fatal write error");
}
return 4;
} fclose(in_file);
fclose(out_file);
return 0;
}
编译链接后没有错误,假如我在f盘有两个txt文件:1.txt,2.txt,如何通过dos窗口将1中内容复制到2中呢?
c.exe f:\1.txt f:\2.txt
回车
#include<iostream>
using namespace std;
int main(int argc,char * argv[])
{
FILE *in_file, *out_file;
char * pstr;
char rec[256];
size_t bytes_in, bytes_out;
if(argc!=3)
{
pstr = strrchr(argv[0],'\\');
cout<<"Usage:\n "<<(pstr != NULL ? ++pstr : argv[0])<<" file1 file2\n";
return 1;
}
in_file=fopen(argv[1],"rb");
if(in_file==NULL)
{
perror(argv[1]);
return 2;
}
out_file=fopen(argv[2],"wb");
if(out_file==NULL)
{
perror(argv[2]);
return 3;
}
while((bytes_in=fread(rec,1,256,in_file))>0)
{
bytes_out=fwrite(rec,1,bytes_in,out_file);
if(bytes_in!=bytes_out)
{
perror("Fatal write error");
return 4;
}
}
fclose(in_file);
fclose(out_file);
return 0;
}
============
从代码上看,应该是:程序 1.txt 2.txt。