在Linux系统下用C语言编写个简单的文件复制程序

ms_star 2009-12-14 10:08:00
在我写了个小程序,但觉得不太完善,想改,又改不好。请各位前辈帮忙,谢谢
我写的程序如下:
#include <stdio.h>
#include <unistd.h>
#inlcude <fcntl.h>
#include <sys/types.h>
main()
{
const char *pathName="f.c";
int in, out,flat;
char buf[1024];
in=open("z.c",O_RDONLY,S_IRUSR);
out=creat(pathName,S_IWUSR);
while((flag=read(in,buf,1024))>0)
{
write(out,buffer,flag)
}
close(in);
close(out);
exit(0);
}
将已经存在的文件内容复制到另一文件中,并输出。就这么简单,想改成另一种写法来实现。
题目要求步骤是这样的:
1。定义需要的各种变量
2.打开一个已有文件
3.创建一个新文件
4.复制文件内容:对一个进行读,对另一个进行写
5.关闭2个文件
大家看看吧,改改,谢谢啊。

...全文
1765 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
no109 2009-12-15
  • 打赏
  • 举报
回复
学习
hittlle 2009-12-15
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define MAX 1024

int main(){
int from, to;
char buf[MAX],n;
if((from=open("file.c",O_RDONLY))<0){
fprintf(stderr,"open error\n");
exit(EXIT_FAILURE);
}
if((to=open("target.c",O_RDWR|O_CREAT,S_IWUSR|S_IRUSR|S_IXUSR))<0){
fprintf(stderr,"open error\n");
exit(EXIT_FAILURE);
}
while((n=read(from,buf,MAX))>0){
if(write(to,buf,n)!=n){
perror("write error\n");
exit(EXIT_FAILURE);
}
}
close(from);
close(to);
return 0;
}
qlong_008 2009-12-15
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 jernymy 的回复:]
加上保护机制,使code更壮些
C/C++ code
#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/stat.h>int main(void)
{constchar*pathName="f.c";intin,out, flag;char buffer[1024];in= open("z.c", O_RDONLY, S_IRUSR);if (-1==in)// 打开文件失败,则异常返回 {
printf("open file z.c error !\n");return-1;
}out= creat(pathName, S_IWUSR);if (-1==in)// 创建文件失败,则异常返回 {
printf("create file %s error !\n", pathName);return-1;
}while ((flag= read(in, buffer,1024))>0)
{
write(out, buffer, flag);
}

close(in);
close(out);
printf("copy file z.t to %s finish !\n", pathName);return0;
}
[/Quote]
if (-1==in)// 创建文件失败,则异常返回 ---
-----》if (-1==out)// 创建文件失败,则异常返回
亦枫Leonlew 2009-12-14
  • 打赏
  • 举报
回复
我的BLOG里有,你可以看看http://hi.baidu.com/vivo01/blog/item/ddea80cf54208d33b600c89b.html
jernymy 2009-12-14
  • 打赏
  • 举报
回复
加上保护机制,使code更壮些

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>

int main(void)
{
const char *pathName = "f.c";
int in, out, flag;
char buffer[1024];

in = open("z.c", O_RDONLY, S_IRUSR);
if (-1 == in) // 打开文件失败,则异常返回
{
printf("open file z.c error !\n");
return -1;
}

out = creat(pathName, S_IWUSR);
if (-1 == in) // 创建文件失败,则异常返回
{
printf("create file %s error !\n", pathName);
return -1;
}

while ((flag = read(in, buffer, 1024)) > 0)
{
write(out, buffer, flag);
}

close(in);
close(out);
printf("copy file z.t to %s finish !\n", pathName);

return 0;
}

  • 打赏
  • 举报
回复
1。定义需要的各种变量
2.打开一个已有文件
3.创建一个新文件
4.复制文件内容:对一个进行读,对另一个进行写
5.关闭2个文件
大家看看吧,改改,谢谢啊。

你上面的代码完全是按照你这5个要求来的,可以了,不需要改成另一种写法。可以结贴啦。
starwalker 2009-12-14
  • 打赏
  • 举报
回复
你这程序编译就通不过啊。
write(out,buffer,flag)

buffer没定义;少分号。

另外,buf可以定义大一点,1k太小了,复制文件的效率会差一点。
main函数最好写成标准形式
int main(int argc, char * argv[])
{
// ...
return 0;
}
bladesoft 2009-12-14
  • 打赏
  • 举报
回复
我也感觉楼主的已经是这个样子了 就是中间把buf写成buffer了
建鼎呓语 2009-12-14
  • 打赏
  • 举报
回复
char cmd[80];
sprintf(cmd, "cp %s %s", srcpath, destpath);
system(cmd);

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧