vim编辑源代码 file1.c
#include “stdio.h”
int main(int argc,char *argv[])
{
FILE *fp1,*fp2;
char ch[maxsize];
if(argc!=3)
{
printf(“command error!\n”);
return -1;// exit(-1);
}
if( (fp1=fopen(argv[1],“r”))==NULL)
{
printf(“file %s cannot open”,argv[1]);
return -1;//exit(-1);
}
if ((fp2=fopen(argv[2],“wa+”))==NULL)
{
printf(“cannot creat file %s”,argv[1]);
return -1;// exit(-1);
}
while(fgets(ch,maxsize,fp1)!=NULL)
fputs(ch,fp2);
fclose(fp1);
fclose(fp2);
return 0;
}
编译 gcc file1 -o file1
最后执行 命令 ./file1 1.txt 2.txt

本文展示了一个使用C语言进行文件复制的示例代码。通过命令行参数接收源文件和目标文件路径,利用fopen、fgets和fputs函数实现文件内容的读取和写入,确保了文件的正确复制。
364

被折叠的 条评论
为什么被折叠?



