您的位置:首页 > 运维架构

文件复制程序 file_copy.c

2013-10-14 10:22 405 查看
#include<stdio.h>
#include<stdlib.h>
#define BUFFER_SIZE 1024
int main(int argc,char ** argv)
{
FILE *fileForm,*fileTo;
char buffer[BUFFER_SIZE]={0};
int length=0;
/*检查输入命令格式是否正确*/
if(argc!=3)
{
printf("Usage:%s fileForm fileTo\n",argv[0]);
exit(0);
}
/*打开源文件*/
fileForm=fopen(argv[1],"rb+");
if(fileForm==NULL)
{
printf("Open File %s Failed\n",argv[1]);
exit(0);
}
/*打开或创建目标文件*/
fileTo=fopen(argv[2],"wb+");
if(fileTo==NULL)
{
printf("Open File %s Failed\n",argv[2]);
exit(0);
}
/*复制文件内容*/
while((length=fread(buffer,1,BUFFER_SIZE,fileForm))>0)
{
fwrite(buffer,1,length,fileTo);
}
/*关闭文件*/
fclose(fileForm);
fclose(fileTo);
return 0;
}


    编译源程序并生成可执行程序file_copy,然后执行程序,将hello.c复制成zhs.c,则编译和运行命令如下:

# gcc file_copy.c -o file_copy
# ./file_copy hello.c zhs.c
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c gcc linux 文件复制
相关文章推荐