我在书上看到一段关于文件复制的代码:#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中呢?

解决方案 »

  1.   

    假设生成的可执行文件名为c.exe,在命令行下输入
    c.exe f:\1.txt f:\2.txt
    回车
      

  2.   

    改为
    #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;
    }
      

  3.   

    编译链接后没有错误,假如我在f盘有两个txt文件:1.txt,2.txt,如何通过dos窗口将1中内容复制到2中呢?
    ============
    从代码上看,应该是:程序 1.txt 2.txt。
      

  4.   

    嗯,4楼帮我修改了代码,确实自己太粗心了,把==号用成了赋值符号=,7楼中也指出了其中的问题,我把return写到了if句外面~~