/*write.c*/
#include
#include
#include
#include
#include
#include
#include
#define MAXSIZE
int main(void)
{
int i,fd,size,len;
char *buf="Hello! I'm writing to this file!";
char buf_r[10];
len = strlen(buf);
if((fd = open("/tmp/hello.c", O_CREAT | O_TRUNC | O_RDWR,0666 ))<0){
perror("open:");
exit(1);
}
else
printf("open file:hello.c %d\n",fd);
if((size = write( fd, buf, len)) < 0){
perror("write:");
exit(1);
}
else
printf("Write:%s\n",buf);
lseek( fd, 0, SEEK_SET );
if((size = read( fd, buf_r, 10))<0){
perror("read:");
exit(1);
}
else
printf("read form file:%s\n",buf_r);
if( close(fd) < 0 ){
perror("close:");
exit(1);
}
else
printf("Close hello.c\n");
exit(0);
}
我的运行结果是在size = read( fd, buf_r, 10))<0 出错了 read 函数不是这样写的吗?
pl_014 于 2013-02-28 17:44:38发表:
C程序可以拿splint来检查一下
deem_passion 于 2013-02-28 16:21:40发表:
好东西
hml1006 于 2013-02-28 13:40:55发表:
我试了一下没问题
lz buf_r数组最后一个不是'\0'
Entertr 于 2013-02-28 11:15:23发表:
不懂这个