C-随机访问
随机访问
int fseek(FILE *stream, long int offset, int whence)
- stream -- 指向 FILE 对象的指针, 该 FILE 对象标识了流.
- offset -- 这是相对 whence 的偏移量, 以字节为单位. 若为负则向前移.
- whence -- 这是表示开始添加偏移 offset 的位置.
- SEEK_SET -- 文件开头
- SEEK_CUR -- 文件指针当前位置
- SEEK_END -- 文件末尾
- 如果成功, 返回0, 否则返回非零值.
FILE* file = fopen("test.txt", "r"); if(file != NULL) { fseek(file, 2, SEEK_SET); putchar(getc(file)); fclose(file); }
当前位置
long int ftell(FILE *stream)
该函数返回位置标识符的当前值. 如果发生错误, 则返回 -1L, 全局变量 errno 被设置为一个正值.
FILE* file = fopen("test.txt", "r"); if(file != NULL) { int len; fseek(file, 0, SEEK_END); len = ftell(file); fclose(file); printf("test.txt 的总大小 = %d 字节\n", len); }
设定位置
FILE* file = fopen("test.txt", "r"); if(file != NULL) { fpos_t pos; // 使用fpos_t存储位置 fgetpos(file, &pos); // 获取位置 fseek(file, -2, SEEK_END); // 移动当前位置 fsetpos(file, &pos); // 设定位置 printf("%ld", ftell(file)); fclose(file); }
本文作者:khrushchefox
本文链接:https://www.cnblogs.com/khrushchefox/p/17369895.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
2022-05-03 SQL Server-包含not exists 谓词的嵌套相关子查询