linux c 文件 read(读) 和 write (写) 代码分析
来源:互联网 发布:扎克伯格老婆知乎 编辑:程序博客网 时间:2024/06/14 12:08
read code:
[root@luozhonghua 03]# cat ex03-read-01.c
/*文件ex03-open-03.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;
/*存放数据的缓冲区*/
char buf[10];
char filename[] = "test.txt";
/*打开文件,如果文件不存在,则报错*/
fd = open(filename,O_RDONLY);
if(-1 == fd){
/*文件已经存在*/
printf("Open file %s failure,fd:%d\n",filename,fd);
} else {
/*文件不存在,创建并打开*/
printf("Open file %s success,fd:%d\n",filename,fd);
}
/*循环读取数据,直到文件末尾或者出错*/
while(size){
/*每次读取10个字节数据*/
size = read(fd, buf,10);
if( -1 == size) {
/*读取数据出错*/
close(fd);/*关闭文件*/
printf("read file error occurs\n");
/*返回*/
return -1;
}else{
/*读取数据成功*/
if(size >0 ){
/*获得size个字节数据*/
printf("read %d bytes:",size);
/*打印引号*/
printf("\"");
/*将读取的数据打印出来*/
for(i = 0;i<size;i++){
printf("%c",*(buf+i));
}
/*打印引号并换行*/
printf("\"\n");
}else{
printf("reach the end of file\n");
}
}
}
return 0;
}
[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 (写) 代码分析
- Linux 文件 open,creat,write,read (C)
- Python读(read)写(write)文件
- Linux -C 的read 和 write函数
- Linux -C 的read 和 write函数
- read write lseek代码分析(非己品)
- linux c通过文件描述符以及write和read方法对文件进行读写
- Python基础-IO编程-读文件read-写文件write
- LINUX C中read,write
- linux 2.6.11内核文件IO的系统调用实现分析(read,write)(转载)
- Linux的C函数open()、read()和write()使用说明
- Linux的C函数open()、read()和write()使用说明
- Linux下read和write函数复制一个文件
- Linux基础----------文件 IO 之 read 和 write (readn 、writen、readline)
- VFS 代码分析(open/read/write)
- Linux下的C语言编程——系统调用read和write函数实现文件拷贝
- read write 调用过程 文件读写 分析
- Linux 文件IO函数(open/close/read/write/lseek)
- 用C语言宏定义寄存器地址解释 和 关键字 volatile
- SEO
- java之this关键字
- jQuery 参考手册 - 选择器
- Nand Flash和Nor Flash 启动区别
- linux c 文件 read(读) 和 write (写) 代码分析
- TS流中从流解析出段和表详细信息
- uva10603 Fill
- android自定义标题
- TS流中从段解析出详细的表信息
- android中ImageView属性scaletype的设置
- 网页设计师一般会去的网站
- [c/c++练习系列]多项式合并问题
- request.getParameter() 和request.getAttribute() 区别