|
read函数的使用
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <unistd.h>
- int main(void)
- {
- //打开指定路径(开发板的路径)下的1.txt文件,通过可读可写的权限
- int fd = open("/mnt/hgfs/share/1.txt",O_RDWR);
- if(fd == -1)
- {
- //在冒号后面打印出错的原因
- perror("打开txt文件失败:");
- return -1;
- }
- //定义的缓冲区,用来保存读取到的数据
- char buf[1024] = {0};
- //读取1.TXT里面的数据
- int ret = read(fd,buf,sizeof(buf));
- if(ret == -1)
- {
- perror("读取数据失败:");
- return -1;
- }
- else
- printf("ret = %d\n",ret);
- printf("buf:%s\n",buf);
- close(fd);
- return 0;
- }
复制代码
|
|