名前空間
変種
操作

fgetpos

From cppreference.com
< c‎ | io
 
 
ファイル入出力
型とオブジェクト
        
関数
ファイルアクセス
(C95)
非書式化入出力
(C95)(C95)
(C95)
(C95)(C95)
(C95)
(C95)

書式付き入力
直接入出力
書式付き出力
ファイルポジショニング
fgetpos
エラーハンドリング
ファイル操作
 
ヘッダー <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、それ以外の場合はゼロ以外の値を返します。

[編集]

#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 関数

[編集] 関連項目

現在のファイル位置指示子を返す
(関数) [編集]
ファイル位置指示子をファイル内の特定の場所に移動する
(関数) [編集]
ファイル位置指示子をファイル内の特定の場所に移動する
(関数) [編集]
English 日本語 中文(简体) 中文(繁體)