当前位置: 首页 > news >正文

ubuntu学习(五)----读取文件以及光标的移动

1、读取文件函数原型介绍

ssize_t read(int fd,void*buf,size_t count)

参数说明:
fd: 是文件描述符
buf:为读出数据的缓冲区;
count:   为每次读取的字节数(是请求读取的字节数,读上来的数据保存在缓冲区buf中,同时文件的当前读写位置向后移)

返回值:

 成功:返回读出的字节数
 失败:返回-1,并设置errno,如果在调用read,之前到达文件末尾,则这次read返回0

2、读取文件函数示例  

打开终端,输入以下指令:

 vi demo2.c

 接着输入如下代码:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>int main()
{int fd;char *buf = "asdfgh!";	fd = open("./file1",O_RDWR);if(fd == -1){printf("open file1 failed\n");fd = open("./file1",O_RDWR|O_CREAT,0600);if(fd > 0){printf("create file1 success!\n");}}printf("open susceess : fd = %d\n",fd);//	ssize_t write(int fd, const void *buf, size_t count);int n_write = write(fd,buf,strlen(buf));if(n_write != -1){printf("write %d byte to file\n",n_write);}char *readBuf;readBuf = (char *)malloc(sizeof(char)*n_write + 1);	
//	  ssize_t read(int fd, void *buf, size_t count);int n_read = read(fd, readBuf, n_write);printf("read %d ,context:%s\n",n_read,readBuf);close(fd);return 0;
}

保存退出后,输入如下指令:

gcc demo2.c 

./a.out

 3、光标移动操作

从运行结果可以看到,并未读取到内容,因为读取时候光标不在最左侧,因此需要进行光标设置。

 光标函数原型:

 off_t lseek(int fd, off_t offset, int whence);
  • 函数参数

    • fd:文件描述符

    • offset:偏移量

    • whence:位置

      • SEEK_SET:The offset is set to offset bytes. offset为0时表示文件开始位置。
      • SEEK_CUR:The offset is set to its current location plus offset bytes. offset为0时表示当前位置。
      • SEEK_END:The offset is set to the size of the file plus offset bytes. offset为0时表示结尾位置
  • 函数返回值

    • 成功返回当前位置到开始的长度
    • 失败返回-1并设置errno

 首先输入如下指令:

vi demo2.c

 输入以下代码:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>int main()
{int fd;char *buf = "asdfghi!";	fd = open("./file1",O_RDWR);if(fd == -1){printf("open file1 failed\n");fd = open("./file1",O_RDWR|O_CREAT,0600);if(fd > 0){printf("create file1 success!\n");}}printf("open susceess : fd = %d\n",fd);int n_write = write(fd,buf,strlen(buf));if(n_write != -1){printf("write %d byte to file\n",n_write);}char *readBuf;readBuf = (char *)malloc(sizeof(char)*n_write + 1);	lseek(fd, 0, SEEK_SET);int n_read = read(fd, readBuf,100);printf("read %d ,context:%s\n",n_read,readBuf);close(fd);return 0;
}

 保存,输入以下指令:

gcc demo2.c

./a.out

 运行结果如下:

 

http://www.lryc.cn/news/140052.html

相关文章:

  • Python 数据分析——matplotlib 快速绘图
  • uniapp小程序位置信息配置
  • 《基于 Vue 组件库 的 Webpack5 配置》1.模式 Mode 和 vue-loader
  • 01.sqlite3学习——数据库概述
  • 视频集中存储/云存储平台EasyCVR国标GB28181协议接入的报文交互数据包分析
  • 容器技术,1. Docker,2. Kubernetes(K8s):
  • Jtti :sql server怎么备份数据库?
  • Stable Diffusion 系列教程 | 打破模型壁垒
  • Cypress 做 e2e 测试,如何在获得某个 checkbox 后先判断它是否被 check 然后再更改它的状态?
  • 基于PIC单片机温度-脉搏-DS18B20温度-液晶12864显示(proteus仿真+源程序)
  • 【C++进阶(一)】STL大法以及string的使用
  • leetcode做题笔记99. 恢复二叉搜索树
  • 24 | 紧跟时代步伐:微服务模式下API测试要怎么做?
  • 【论文阅读】POIROT:关联攻击行为与内核审计记录以寻找网络威胁(CCS-2019)
  • K8S cluster with multi-masters on Azure VM
  • 初阶c语言:趣味扫雷游戏
  • JVM——内存模型
  • java八股文面试[JVM]——元空间
  • 科技云报道:云计算下半场,公有云市场生变,私有云风景独好
  • Oracle 如何给大表添加带有默认值的字段
  • 记录Taro大坑2丢失api无法启动
  • Java-Maven-解决maven deploy时报 401 Reason Phrase Unauthorized 错误
  • 【数据结构】 栈(Stack)的应用场景
  • 人力资源小程序的设计原则与实现方法
  • 检查Javascript对象数组中是否存在对象值,如果没有向数组添加新对象
  • UG\NX二次开发 使用录制功能录制操作记录时,如何设置默认的开发语言?
  • 【业务功能篇83】微服务SpringCloud-ElasticSearch-Kibanan-docke安装-应用层实战
  • VBJSON报错:缺少:语句结束
  • Docker安装ES+kibana8.9.1
  • 12. Oracle中case when详解