Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      fsetpos

      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 fsetpos(FILE* stream,constfpos_t* pos);

      Sets the file position indicator and the multibyte parsing state (if any) for the file streamstream according to the value pointed to bypos.

      Besides establishing new parse state and position, a call to this function undoes the effects ofungetc and clears the end-of-file state, if it is set.

      If a read or write error occurs, the error indicator (ferror) for the stream is set.

      Contents

      [edit]Parameters

      stream - file stream to modify
      pos - pointer to afpos_t object to use as new value of file position indicator

      [edit]Return value

      0 upon success, nonzero value otherwise.

      [edit]Notes

      After seeking to a non-end position in a wide stream, the next call to any output function may render the remainder of the file undefined, e.g. by outputting a multibyte sequence of a different length.

      [edit]Example

      fsetpos with error checking

      Run this code
      #include <stdio.h>#include <stdlib.h> int main(void){// Prepare an array of FP (floating-point) values.#define SIZE 5double A[SIZE]={1.0,2.0,3.0,4.0,5.0};// Write array to a file.FILE* fp=fopen("test.bin","wb");fwrite(A,sizeof(double),SIZE,fp);fclose(fp); // Read the FP values into array B.double B[SIZE];    fp=fopen("test.bin","rb");fpos_t pos;if(fgetpos(fp,&pos))// current position: start of file{perror("fgetpos()");fprintf(stderr,"fgetpos() failed in file %s at line # %d\n",                __FILE__, __LINE__-3);exit(EXIT_FAILURE);} int ret_code=fread(B,sizeof(double),1,fp);// read one FP value// current position: after reading one f-p valueprintf("%.1f; read count = %d\n", B[0], ret_code);// print one FP value and ret_code if(fsetpos(fp,&pos))// reset current position to start of file{if(ferror(fp)){perror("fsetpos()");fprintf(stderr,"fsetpos() failed in file %s at line # %d\n", __FILE__,                    __LINE__-5);exit(EXIT_FAILURE);}}     ret_code=fread(B,sizeof(double),1, fp);// reread first FP valueprintf("%.1f; read count = %d\n", B[0], ret_code);// print one FP value and ret_codefclose(fp); returnEXIT_SUCCESS;}

      Possible output:

      1.0; read count = 11.0; read count = 1

      [edit]References

      • C23 standard (ISO/IEC 9899:2024):
      • 7.21.9.3 The fsetpos function (p: TBD)
      • C17 standard (ISO/IEC 9899:2018):
      • 7.21.9.3 The fsetpos function (p: TBD)
      • C11 standard (ISO/IEC 9899:2011):
      • 7.21.9.3 The fsetpos function (p: 337)
      • C99 standard (ISO/IEC 9899:1999):
      • 7.19.9.3 The fsetpos function (p: 303)
      • C89/C90 standard (ISO/IEC 9899:1990):
      • 4.9.9.3 The fsetpos function

      [edit]See also

      gets the file position indicator
      (function)[edit]
      returns the current file position indicator
      (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/fsetpos&oldid=178687"

      [8]ページ先頭

      ©2009-2025 Movatter.jp