Namespaces
Variants

fgetpos

ja.cppreference.net より
 
 
ファイル入出力
型とオブジェクト
        
関数
ファイルアクセス
(C95)
書式なし入出力
(C95)(C95)
(C95)
(C95)(C95)
(C95)
(C95)

書式付き入力
 
ヘッダ <stdio.h>
int fgetpos( FILE* stream, fpos_t* pos );
で定義(C99 まで)
int fgetpos( FILE* restrict stream, fpos_t* restrict pos );
(C99 以降)

ファイルストリーム stream のファイル位置インジケータと、現在のパース状態(あれば)を取得し、pos が指すオブジェクトに格納します。格納された値は、fsetpos への入力としてのみ意味を持ちます。

引数

stream - 調査するファイルストリーム
pos - ファイル位置インジケータを格納する fpos_t オブジェクトへのポインタ

戻り値

0 成功時は 0、そうでなければ非ゼロ値。

例

#include <assert.h>
#include <stdio.h>
#include <stdlib.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");
}

出力:

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 関数 (p: TBD)
  • 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 関数

関連項目

現在のファイル位置インジケータを返す
(関数)
ファイル位置インジケータをファイル内の特定の位置に移動する
(関数)
ファイル位置インジケータをファイル内の特定の位置に移動する
(関数)