当前位置:网站首页>文件的随意读写
文件的随意读写
2022-08-09 05:13:00 【潜水少年请求出战】
1. fseek
根据文件指针的位置和偏移量来定位文件指针。
int fseek ( FILE * stream, long int offset, int origin );
先贴一段代码:#include<stdio.h> //fseek. int main() { //打开文件 FILE* pf = fopen("example.txt", "w"); if (pf == NULL) { perror("pf"); return 1; } //写文件 fputs("abcdef", pf); fseek(pf, 3, SEEK_SET);//文件开始位置 fputs("cba", pf); //关闭文件 fclose(pf); pf = NULL; return 0; }fseek第一个参数是流就是pf;第二个参数就是偏移量;第三个就是指针的位置(SEEK_CUR文件指针当前位置;SEEK_END文件末尾位置)。
2.ftell
返回文件指针相对于起始位置的偏移量
long int ftell ( FILE * stream );
#include<stdio.h> int main() { //打开文件 FILE* pf = fopen("example.txt", "r"); if (pf == NULL) { perror("pf"); return 1; } //读文件 fseek(pf, -2, SEEK_END);//改变指针位置。 long ret = ftell(pf); printf("%ld", ret); //关闭文件 fclose(pf); pf = NULL; return 0; }
3.rewind
让文件指针的位置回到文件的起始位置
void rewind ( FILE * stream );#include<stdio.h> //fseek. int main() { //打开文件 FILE* pf = fopen("example.txt", "w"); if (pf == NULL) { perror("pf"); return 1; } //写文件 fputs("abcdef", pf); fseek(pf, 3, SEEK_SET);//文件开始位置 fputs("cba", pf); rewind(pf); long ret = ftell(pf); printf("%ld", ret);//0 //关闭文件 fclose(pf); pf = NULL; return 0; }
边栏推荐
- 数据库设计---三范式和反范式设计
- P8462 「REOI-1」奶油蛋糕
- 22-08-08 西安 尚医通(04)MongoDB命令、MongoTemplate、MongoRepository
- 软件测试的方法详细介绍
- [Developers must see] [push kit] Collection of typical problems of push service service 2
- 无法通过头文件中的宏定义或全局变量修改动态库中的参数
- Faced with risk control, what should Amazon do when evaluating self-supporting accounts?
- dsafasfdasfasf
- 通讯录(文件版)(C语言)(VS)
- 想要精准营销,从学习搭建一套对的标签体系开始丨DTVision分析洞察篇
猜你喜欢

屏:全贴合工艺之GF、GF2、G1F1、GG、TOL

FastDFS快速使用及介绍

mysql content does not exist error

什么是ReFi?

Still don't know what business intelligence (BI) is?After reading this article, you will understand

图解LeetCode——761. 特殊的二进制序列(难度:困难)

Harmony OS ets ArkUI 】 【 】 development create a view and building layout

力扣349-两个数组的交集——HashSet

Software testing method is introduced in detail

【零基础玩转BLDC系列】无刷直流电机闭环控制与软件架构
随机推荐
面向6G的欠采样相移键控可见光调制方案
初识二叉树
力扣349-两个数组的交集——HashSet
JDBC_PreparedStatement预编译对象
多字段关联校验
【Harmony OS】【FAQ】Hongmeng Questions Collection 1
3.3V控制输出5V的方法
【Harmony OS】【ArkUI】ets开发 创建视图与构建布局
【luogu U142356】Suffix of the Brave (SA) (Chairman Tree) (2 points)
STM32定时器输入捕获频率(cube)
The request was rejected because the URL contained a potentially malicious String “//“
[Harmony OS] [ARK UI] ETS context basic operations
aur安装报错一个或多个文件没有通过有效性检查!
ELTEK电源维修SMPS5000SIL整流器模块故障分析及特点
使用Redis zset做消息队列
Openresty执行lua脚本
通讯录(文件版)(C语言)(VS)
软件测试的发展趋势
学习一下 常说的节流
什么是通用微处理器、单片机、DSP芯片、嵌入式系统?


