关于linux 下read/write函数使用及注意事项
来源:互联网 发布:网络五大鬼畜舞曲 编辑:程序博客网 时间:2024/05/29 08:37
read函数从打开的设备或文件中读取数据。#include <unistd.h> ssize_t read(int fd, void *buf, size_t count); 返回值:成功返回读取的字节数,出错返回-1并设置errno,如果在调read之前已到达文件末尾,则这次read返回0参数count是请求读取的字节数,读上来的数据保存在缓冲区buf中,同时文件的当前读写位置向后移。注意这个读写位置和使用C标准I/O库时的读写位置有可能不同,这个读写位置是记在内核中的,而使用C标准I/O库时的读写位置是用户空间I/O缓冲区中的位置。
读写函数使用倒不是很难,我这里强调的是read/write函数调用后,读写文件的指针已经移动,不是开始位置了;
例如:
write(fd,"1",1);
read(fd,&a,1);
这时候读出的变量a是\n;
printf("%d",a);
结果是10
因为write后指针已经移动,read的是1后面的\n;
可以这样
write(fd,"1",1);
lseek(fd, 0, SEEK_SET);
read(fd,&a,1);
printf("%d",a);
结果是49,即,字符 1.
0 0
- 关于linux 下read/write函数使用及注意事项
- linux下 open() write() read() close函数
- linux下 open() write() read() close函数
- linux下 open() write() read() close函数
- linux下 open() write() read() close函数
- linux下 open() write() read() close函数
- Linux下的read,write函数
- linux下 open() write() read() close函数
- linux下 open() write() read() close函数
- linux下 open() write() read() close函数
- linux下 open() write() read() close函数
- linux下的open()read()write()close()lseek()函数
- [转载]linux下 open() write() read() close函数
- 深入理解linux下write()和read()函数
- Linux下read和write函数复制一个文件
- linux下 open() write() read() close函数讲解
- Linux下write、read方式
- linux下read/write编程
- react-native 启动服务器时候报错Could not install the app on the device, read the error above for details.
- Android注解框架对比
- js跨域请求的个人见解
- linux下监听的配置 采用netca命令调用图形界面进行配置
- Lakes in Berland(CF #375 Div. 2)
- 关于linux 下read/write函数使用及注意事项
- css修炼宝典
- background-color覆盖范围
- 机器学习(周志华)2.1-2.9习题解答
- 网络通信
- 安卓缓存LruCache、DiskLruCache、SharedPreference、SqlLite-1LruCache
- hibernate查询指定字段示例
- windows下安装memcached,报错:Failed to ignore SIGHUP: No error
- 数据可视化——matplotlib.pyplot