首页 > 试题广场 >

编写一个文件复制程序。程序需要从命令行获得源文件名和目的文件

[问答题]

编写一个文件复制程序。程序需要从命令行获得源文件名和目的文件名。尽可能使用标准I/O和二进制模式。

推荐
#include<stdio.h>
#include<stdlib.h>
int main(int argc,char *argv[])
{
 int ch;
 FILE *source,*destination;
 if ( (source = fopen(argv[1],"rb")) == NULL)
 {
 printf ("Can't open %s\n", argv[1]);
 exit(1);
 }
 if ( (destination = fopen(argv[2],"wb")) == NULL)
 {
 printf ("Can't open %s\n", argv[2]);
 exit(1);
 }
 while ((ch = getc(source)) != EOF)
 putc(ch,destination);
 fclose(source);
 fclose(destination);
 printf ("copy finished\n");
 return 0;
}

发表于 2018-05-05 21:56:38 回复(0)