名稱空間
變體
操作

fgetpos

來自 cppreference.com
< c‎ | io
 
 
檔案輸入/輸出
型別和物件
        
函式
檔案訪問
(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,否則返回非零值。

[編輯] 示例

#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: 待定)
  • C17 標準 (ISO/IEC 9899:2018)
  • 7.21.9.1 fgetpos 函式 (p: 待定)
  • 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 函式

[編輯] 另請參閱

返回當前檔案位置指示器
(函式) [編輯]
將檔案位置指示器移動到檔案中特定位置
(函式) [編輯]
將檔案位置指示器移動到檔案中特定位置
(函式) [編輯]
有關 fgetposC++ 文件