首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

fgetpos

在头文件<stdio.h>中定义

?

?

int fgetpos(FILE * stream,fpos_t * pos);

?

(直到C99)

int fgetpos(FILE * restrict stream,fpos_t * restrict pos);

?

(自C99以来)

获取文件流的文件位置指示器和当前解析状态(如果有)stream并将它们存储在指向的对象中pos。存储的值只对输入有意义fsetpos

参数

-

文件流来检查

岗位

-

指向fpos_t对象以存储文件位置指示符的指针

返回值

?0? 一旦成功,否则非零值。

代码语言:javascript
复制
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
 
int main(void)
{
    // prepare a file holding 4 values of type 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);
 
    // demo using fsetpos to return to the beginning of a file
    fp = fopen("test.bin", "rb");
    fpos_t pos;
    fgetpos(fp, &pos);               // store start of file in pos
    double d;
    rc = fread(&d, sizeof d, 1, fp); // read the first double
    assert(rc == 1);
    printf("First value in the file: %.1f\n", d);
    fsetpos(fp,&pos);                 // move file position back to the start of the file
    rc = fread(&d, sizeof d, 1, fp);  // read the first double again
    assert(rc == 1);
    printf("First value in the file again: %.1f\n", d);
    fclose(fp);
 
    // demo error handling
    rc = fsetpos(stdin, &pos);
    if(rc) perror("could not fsetpos stdin");
}

输出:

代码语言:javascript
复制
First value in the file: 1.1
First value in the file again: 1.1
could not fsetpos stdin: Illegal seek

参考

  • C11标准(ISO / IEC 9899:2011):
    • 7.21.9.1 fgetpos函数(p:336)
  • C99标准(ISO / IEC 9899:1999):
    • 7.19.9.1 fgetpos函数(p:302)
  • C89 / C90标准(ISO / IEC 9899:1990):
    • 4.9.9.1 fgetpos函数

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com