Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      fgetpos

      From cppreference.com
      <c‎ |io
       
       
      File input/output
      Types and objects
      Functions
      File access
      Unformatted input/output
      (C95)(C95)
      (C95)
      (C95)(C95)
      (C95)
      (C95)

      Formatted input
       
      Defined in header<stdio.h>
      int fgetpos(FILE*          stream,fpos_t*          pos);
      (until C99)
      int fgetpos(FILE*restrict stream,fpos_t*restrict pos);
      (since C99)

      Obtains the file position indicator and the current parse state (if any) for the file streamstream and stores them in the object pointed to bypos. The value stored is only meaningful as the input tofsetpos.

      Contents

      [edit]Parameters

      stream - file stream to examine
      pos - pointer to afpos_t object to store the file position indicator to

      [edit]Return value

      0 upon success, nonzero value otherwise.

      [edit]Example

      Run this code
      #include <assert.h>#include <stdio.h>#include <stdlib.h> int main(void){// prepare a file holding 4 values of type doubleenum{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 posdouble d;    rc=fread(&d,sizeof d,1, fp);// read the first doubleassert(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 againassert(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");}

      Output:

      First value in the file: 1.1First value in the file again: 1.1could not fsetpos stdin: Illegal seek

      [edit]References

      • C23 standard (ISO/IEC 9899:2024):
      • 7.21.9.1 The fgetpos function (p: TBD)
      • C17 standard (ISO/IEC 9899:2018):
      • 7.21.9.1 The fgetpos function (p: TBD)
      • C11 standard (ISO/IEC 9899:2011):
      • 7.21.9.1 The fgetpos function (p: 336)
      • C99 standard (ISO/IEC 9899:1999):
      • 7.19.9.1 The fgetpos function (p: 302)
      • C89/C90 standard (ISO/IEC 9899:1990):
      • 4.9.9.1 The fgetpos function

      [edit]See also

      returns the current file position indicator
      (function)[edit]
      moves the file position indicator to a specific location in a file
      (function)[edit]
      moves the file position indicator to a specific location in a file
      (function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=c/io/fgetpos&oldid=178252"

      [8]ページ先頭

      ©2009-2025 Movatter.jp