read code:
[root@luozhonghua 03]# cat ex03-read-01.c
/*文件ex03-open-03.c,}
[root@luozhonghua 03]# ./ex03-read-01
Open file test.txt success,fd:3
read 10 bytes:"aaaaaaaaaa"
read 10 bytes:"aaaaaaaaaa"
read 10 bytes:"aaaaaaaaaa"
read 5 bytes:"aaaa
"
reach the end of file
-----write
[root@luozhonghua 03]# cat ex03-write-01.c
/*文件ex03-write-01.c,
O_CREAT和O_EXCL的使用*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
int main(void)
{
int fd = -1,i;
ssize_t size = -1;
int input = 0;
/*存放数据的缓冲区*/
char buf[]="quick brown fox jumps over the lazy dog";
char filename[] = "test.txt";
/*打开文件,如果文件不存在,则报错*/
fd = open(filename,O_RDWR|O_TRUNC);
if(-1 == fd){
/*文件已经存在*/
printf("Open file %s failure,fd:%d\n",filename,fd);
} else {
/*文件不存在,创建并打开*/
printf("Open file %s success,fd:%d\n",filename,fd);
}
/*将数据写入到文件test.txt中*/
size = write(fd, buf,strlen(buf));
printf("write %d bytes to file %s\n",size,filename);
/*关闭文件*/
close(fd);
return 0;
}
[root@luozhonghua 03]# cat text.txt
cat: text.txt: No such file or directory
[root@luozhonghua 03]# cat test.txt
quick brown fox jumps over the lazy dog
linux c 文件 read(读) 和 write (写) 代码分析,布布扣,bubuko.com
linux c 文件 read(读) 和 write (写) 代码分析
原文地址:http://blog.csdn.net/luozhonghua2014/article/details/38356899