4.文件---读文件read
1.基础知识
(1)man 2 read
fd:指向用open打开的文件
read(fd,buff,count);从fd指向的文件读取count字节的数据放到缓存buff中。
(2)man malloc
发现使用malloc要使用#include<stdlib.h>这个头文件
(3)lseek
作用:将文件读写指针相对whence移动offset个字节
man lseek
offset是偏移值
whence是位置
whence的3个参数:SEEL_SET指向文件的头;SEEK_END指向文件尾巴;SEEK_CUR指向当前位置
off_t lseek(int fd, off_t offset, int whence);//使用这句代码可以配置指针指向文件的头
lseek()的返回值是偏移量大小(字节)
2.使用read
(1)错误例子
问题:读不到任何数据
原因:在执行 int n_write = write( fd,buf,strlen(buf));
后,指针指向字符串末尾。此时指向read,是从内存上的字符串所在空间的后面开始读,读到的内容不是字符串,是空的。
解决办法:关闭文件(close)再重新打开(open);或者把指针移动到字符串首地址。
(2)正确用法
关闭文件(close)再重新打开(open)
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
int main()
{int fd;char *buf = "yrx0203";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("creat file1 success\n");}printf("open file1 success\n");// 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 file1 \n", n_write);}//关闭再重新打开,使指针指向字符串首地址close(fd);fd = open("./file1",O_RDWR);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 ,contex:%s\n",n_read,readBuf);close(fd);return 0;
}
把指针移动到字符串首地址(将光标移动到文件的头)。
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>int main()
{int fd;char *buf = "yrx0203";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("creat file1 success\n");}printf("open file1 success\n");// 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 file1 \n", n_write);}// close(fd);
// fd = open("./file1",O_RDWR);// off_t lseek(int fd, off_t offset, int whence);char *readBuf;readBuf = (char *)malloc(sizeof(char)*n_write+1);//ssize_t read(int fd, void *buf, size_t count);lseek(fd, 0, SEEK_SET);int n_read = read(fd, readBuf, n_write);printf("read %d ,contex:%s\n",n_read,readBuf);close(fd);return 0;
}
注意:
可以将
lseek(fd, 0, SEEK_SET);//将光标移动到文件头
改写成
lseek(fd, -7, SEEK_CUR);//将光标从当前位置向前移动7字节(因为字符串长度就是7字节)
其中-7代表向前移动7字节;
7代表向后移动7字节。
(3)通过lseek()读文件大小
刚才向file1写入了“yrx0203”7个字节
通过如下函数,可读取file1的大小
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>int main()
{int fd;fd = open("./file1",O_RDWR);int File_size = lseek(fd,0,SEEK_END);printf("file_size %d",File_size);close(fd);return 0;}
~
4.文件---读文件read相关推荐
- Java 支付宝对账功能(查询+文件下载+解压+遍历文件+读文件)
Java 支付宝对账功能(查询+文件下载+解压+遍历文件+读文件) **需求** **流程** 1 .调用支付宝接口, 获取zip 下载地址 2.工具类代码 3.目录 4.开发环境 5.更新实际收益到 ...
- C# 支付宝对账功能(查询+文件下载+解压+遍历文件+读文件)
C# 支付宝对账功能(查询+文件下载+解压+遍历文件+读文件) **需求** **流程** 1 .调用支付宝接口, 获取zip 下载地址 2.工具代码 需求 定时任务:每天统计昨天的公司支付宝账户实际 ...
- python写文件读文件-Python 实例:读写文件
原标题:Python 实例:读写文件 读写文件是最常见的IO操作.内置了读写文件的函数,用法和的读写文件非常类似.在磁盘上读写文件的功能都是由提供的,现代不允许普通的程序直接操作磁盘,所以,读写文件就 ...
- python写文件读文件-Python文件读写
在本章中将介绍Python 3中可用的所有基本文件读取I/O功能.有关更多功能,请参考标准Python文档. 打印到屏幕 产生输出的最简单方法是使用print语句,可以传递零个或多个由逗号分隔的表达式 ...
- python写文件读文件-Python 读写文件和file对象的方法(推荐)
1.open 使用open打开文件后一定要记得调用文件对象的close()方法.比如可以用try/finally语句来确保最后能关闭文件. file_object = open('thefile.tx ...
- 利用CStdioFile类实现写文件读文件(mfc)
文章目录 1.主要函数 1.1读文件 1.2写文件 2.补充知识 3.说明 二话不说,先上代码! 1.主要函数 1.1读文件 // TODO: 在此添加控件通知处理程序代码CFileDialog dl ...
- python写文件读文件-python--文件流读写
在讲述fileinput模块之前,首先说一下python内置的文件API-open()函数以及与其相关的函数. 我这里主要讲讲其中四个比较重要和常用的方法,更多的方法,可以参考:菜鸟教程http:// ...
- python写文件读文件-python(文件读写)
%%writefile test.txt 先自己写一个模块. 这是一个中文文档 第二行 第三行 第四行 读这个文件有两种方法: 可以是f = open("test.txt") ...
- 支付宝(查询对账单下载地址+文件下载+解压+遍历文件+读文件)
1 .调用支付宝接口, 获取zip 下载地址 package com.ycmedia.task;import com.alibaba.fastjson.JSON; import com.alibaba ...
最新文章
- clientdataset 用法
- 强者愈强!疫情拉大“数据资产”贫富差距,顶级公司数据建设靠什么
- 灵动微电子逐飞 智能车支持计划汇总
- EAS中的管理单元、组织单元理解
- linux分辨率和用户有关吗,Linux系统在高分屏非正常分辨率显示
- hibernate实现多变联合查询
- Win7搭建NodeJs开发环境以及HelloWorld展示—图解
- 制作nginx的spec分享
- 风变编程python笔记_自学Python和风变编程
- 前端通过jqplot绘制折线图
- [原创]spring及springmvc精简版--继承数据源,声明式事物
- 210.课程表II(力扣leetcode) 博主可答疑该问题
- c语言编程悬臂梁受力分析,悬臂梁-静力学分析(
- DevExpress项目升级总结
- tig只看某个作者的提交
- 解决问题(七)——jsf+spring+hibernate整合(一)
- Vue小demo—美团注册页面
- python中如何进行测试
- 税务UKey开票软件 V1.0.22_ZS_20221231 版本数据库分析
- h5页面自定义字体_H5页面视觉设计中的字体有哪几种