linux下的简单c 语言程序 求助

pshlcysz 2011-05-20 08:57:28
#include "stdio.h"
#include "stdlib.h"
#include "fcntl.h"
#include "sys/types.h"
#include "sys/stat.h"

int main(void){
int fp1,fp2;
int size;
char buffer[80];
if((fp1=open("abc.txt",O_RDONLY))<0){
printf("this file is not exit!");
exit(0);
}
if((fp2=open("cd.txt",O_WRONLY|O_CREAT))<0){
printf("overflow!");
exit(0);
}
while(fp1!=-1){
size=read(fp1,buffer,sizeof(buffer));
write(fp2,buffer,size);
}
close(fp1);
close(fp2);
}


这是我写的c语言程序 因为对linux不是很熟悉 所以也不是很清楚 到底哪里需要修改 请各位大侠指点
...全文
133 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
pshlcysz 2011-05-21
  • 打赏
  • 举报
回复
好了 朋友们 我的问题解决了 我最后一次贴出的程序无误 主要是没有权限 像上面一个朋友说的 切换到root 就Ok了 谢谢诸位
W170532934 2011-05-21
  • 打赏
  • 举报
回复
为什么我在fedore下怎么没有错???
jianghandaxue 2011-05-21
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 pshlcysz 的回复:]
I have already changed the program like follows,but it always display the information :overflow when I execute the program. Is there anyone who can tell me why ? Thank you ! Because now I am working i……
[/Quote]



UP
pshlcysz 2011-05-21
  • 打赏
  • 举报
回复
I have already changed the program as follows,but it still display the message:overflow

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

int main(void){
int fp1,fp2;
int size;
char buffer[80];
if((fp1=open("abc.txt",O_RDONLY))<0){
printf("abc.txt is not exit!");
exit(1);
}
if((fp2=open("cd.txt",O_WRONLY|O_CREAT,00700))<0){
printf("overflow!\n");
exit(1);
}
while((size=read(fp1, buffer, sizeof(buffer)) ) > 0){
if((write(fp2,buffer,size))!=size){
perror("write error");
};
}
close(fp1);
close(fp2);
printf("ending");
return 1;
}
dreamer_shax 2011-05-21
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 ncu571633 的回复:]

size=read(fp1,buffer,sizeof(buffer));
write(fp2,buffer,size);

read 为什么是读sizeof(buffer),应该是去读80,buffer的大小,而不是buffer type的大小。
[/Quote]

sizeof(数组名)就是该数组的大小。
yyysss520 2011-05-21
  • 打赏
  • 举报
回复
permission denied 找个是权限问题啊 你切成root用户
「已注销」 2011-05-21
  • 打赏
  • 举报
回复

while(fp1>0)

fp1是大于0的,不是死循环了么
「已注销」 2011-05-21
  • 打赏
  • 举报
回复
size=read(fp1,buffer,sizeof(buffer));
write(fp2,buffer,size);

read 为什么是读sizeof(buffer),应该是去读80,buffer的大小,而不是buffer type的大小。
pshlcysz 2011-05-21
  • 打赏
  • 举报
回复
I have already changed the program like follows,but it always display the information :overflow when I execute the program. Is there anyone who can tell me why ? Thank you ! Because now I am working in Linux and the languages is English ,I have to tell you the problem in English.please forgive me !


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

int main(void){
int fp1,fp2;
int size;
char buffer[80];
if((fp1=open("abc.txt",O_RDONLY))<0){
printf("abc.txt is not exit!");
exit(0);
}
if((fp2=open("cd.txt",O_WRONLY|O_CREAT,0666))<0){
printf("overflow!");
exit(0);
}
while(fp1>0){
size=read(fp1,buffer,sizeof(buffer));
write(fp2,buffer,size);
}
close(fp1);
close(fp2);
printf("ending");
return 1;
}
Sun_Phoenix 2011-05-20
  • 打赏
  • 举报
回复

#include "stdio.h"//用双引号干什么<stdio.h>
#include "stdlib.h"
#include "fcntl.h"
#include "sys/types.h"
#include "sys/stat.h"

int main(void){
int fp1,fp2;
int size;
char buffer[80];

if((fp1 = open("abc.txt", O_RDONLY)) < 0){
printf("this file is not exit!");
exit(0);//exit(1);0一般表示正确
}
//加O_CREAT需要加mode
if((fp2=open("cd.txt", O_WRONLY |O_CREAT| O_TRUNC, 0666))<0){
printf("overflow!");
exit(0);//同上
}
//不要这个了while(fp1 != -1)
while ((size=read(fp1, buffer, sizeof(buffer)) > 0){
if (write(fp2, buffer, size) != size)) {
perror("write error");
}
}

close(fp1);
close(fp2);
}

不知道对不对!改了下
pshlcysz 2011-05-20
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 anyidan 的回复:]
什么问题?
[/Quote]

执行的时候显示:permission denied

那个我在头文件里加了#include "unistd.h"
AnYidan 2011-05-20
  • 打赏
  • 举报
回复
什么问题?
DB2 求助编辑百科名片 DB2是IBM出口的一系列关系型数据库管理系统,分别在不同的操作系统平台上服务。虽然DB2产品是基于UNIX的系统和个人计算机操作系统,在基于UNIX系统和微软在windows系统下的Access方面,DB2追寻了ORACLE的数据库产品。 目录 综述 DB2的历史 DB2的数据复制与迁移方法 DB2数据库常用命令集 DB2 Express-C 精简版 MD游戏DB2 展开 编辑本段综述 简介   IBM公司研制的一种关系型数据库系统。DB2主要应用于大型应用系统,具有较好的可伸缩性,可支持从大型机到单用户环境,应用于OS/2、Windows等平台下。 DB2提供了高层次的数据利用性、完整性、安全性、可恢复性,以及小规模到大规模应用程序的执行能力,具有与平台无关的基本功能和SQL命令。DB2采用了数据分级技术,能够使大型机数据很方便地下载到LAN数据库服务器,使得客户机/服务器用户和基于LAN的应用程序可以访问大型机数据,并使数据库本地化及远程连接透明化。 它以拥有一个非常完备的查询优化器而著称,其外部连接改善了查询性能,并支持多任务并行查询。 DB2具有很好的网络支持能力,每个子系统可以连接十几万个分布式用户,可同时激活上千个活动线程,对大型分布式应用系统尤为适用。 DB2(3张)   除了它可以提供主流的OS/390和VM操作系统,以及中等规模的AS/400系统之外,IBM还提供了跨平台(包括基于UNIX的LINUX,HP-UX,SunSolaris,以及SCOUnixWare;还有用于个人电脑的OS/2操作系统,以及微软的Windows 2000和其早期的系统)的DB2产品。DB2数据库可以通过使用微软的开放数据库连接(ODBC)接口,Java数据库连接(JDBC)接口,或者CORBA接口代理被任何的应用程序访问。   2006年7月14日,IBM全球同步发布了一款具有划时代意义的数据库产品——DB2 9(“DB2”是IBM数据库产品系列的名称)。而这款新品最大特点即是率先实现了可扩展标记语言(XML)和关系数据间的无缝交互,而无需考虑数据的格式、平台或位置。 DB2的前世今生和未来   对于每个最终站在奖台上泪水盈面的奥运冠军来说,为此刻他或她也许已经付出了5年甚至10年的艰苦努力。相比这些人类的冠军们,这个世界还有另外一种意义上的冠军,它们虽没有泪水,却依然在历史上留下了非凡的轨迹—DB2就是这类冠军中的一员。这个数据库领域里当之无愧的冠军,已用了足足25年来描绘它的轨迹。

69,381

社区成员

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

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