fgetpos
From cppreference.net
File input/output
| Types and objects | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
定义于头文件
<stdio.h>
|
||
| (C99前) | ||
| (C99起) | ||
获取文件流 stream 的文件位置指示器及当前解析状态(如果存在),并将其存储到 pos 所指向的对象中。存储的值仅作为 fsetpos 的输入时才有意义。
目录 |
参数
| stream | - | 要检查的文件流 |
| pos | - | 指向 fpos_t 对象的指针,用于存储文件位置指示器 |
返回值
0 表示成功,非零值表示失败。
示例
运行此代码
#include <assert.h> #include <stdio.h> #include <stdlib.h> int main(void) { // 准备包含4个double类型值的文件 enum {SIZE = 4}; FILE* fp = fopen("test.bin", "wb"); assert(fp); int rc = fwrite((double[SIZE]){1.1, 2.2, 3.3, 4.4}, sizeof(double), SIZE, fp); assert(rc == SIZE); fclose(fp); // 演示使用fsetpos返回到文件开头 fp = fopen("test.bin", "rb"); fpos_t pos; fgetpos(fp, &pos); // 将文件起始位置存入pos double d; rc = fread(&d, sizeof d, 1, fp); // 读取第一个double值 assert(rc == 1); printf("First value in the file: %.1f\n", d); fsetpos(fp,&pos); // 将文件位置移回文件起始处 rc = fread(&d, sizeof d, 1, fp); // 再次读取第一个double值 assert(rc == 1); printf("First value in the file again: %.1f\n", d); fclose(fp); // 演示错误处理 rc = fsetpos(stdin, &pos); if (rc) perror("could not fsetpos stdin"); }
输出:
First value in the file: 1.1 First value in the file again: 1.1 could not fsetpos stdin: Illegal seek
参考文献
- C23 标准 (ISO/IEC 9899:2024):
-
- 7.21.9.1 fgetpos 函数 (p: TBD)
- C17 标准 (ISO/IEC 9899:2018):
-
- 7.21.9.1 fgetpos 函数 (页: TBD)
- C11 标准 (ISO/IEC 9899:2011):
-
- 7.21.9.1 fgetpos 函数 (p: 336)
- C99标准(ISO/IEC 9899:1999):
-
- 7.19.9.1 fgetpos函数(页码:302)
- C89/C90 标准 (ISO/IEC 9899:1990):
-
- 4.9.9.1 fgetpos 函数
参考
|
返回当前文件位置指示器
(function) |
|
|
将文件位置指示器移动到文件中的特定位置
(function) |
|
|
将文件位置指示器移动到文件中的特定位置
(function) |
|
|
C++ documentation
for
fgetpos
|
|