From 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Sun, 7 Apr 2024 13:41:34 -0500 Subject: new repository --- devdocs/c/io%2Ffsetpos.html | 73 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 devdocs/c/io%2Ffsetpos.html (limited to 'devdocs/c/io%2Ffsetpos.html') diff --git a/devdocs/c/io%2Ffsetpos.html b/devdocs/c/io%2Ffsetpos.html new file mode 100644 index 00000000..3ef15100 --- /dev/null +++ b/devdocs/c/io%2Ffsetpos.html @@ -0,0 +1,73 @@ +

fsetpos

Defined in header <stdio.h>
int fsetpos( FILE *stream, const fpos_t *pos );
+

Sets the file position indicator and the multibyte parsing state (if any) for the file stream stream according to the value pointed to by pos.

+

Besides establishing new parse state and position, a call to this function undoes the effects of ungetc 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.

+

Parameters

+ + +
stream - file stream to modify
pos - pointer to a fpos_t object to use as new value of file position indicator

Return value

​0​ upon success, nonzero value otherwise.

+

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.

+

Example

+

fsetpos with error checking

+
#include <stdio.h>
+#include <stdlib.h>
+ 
+int main(void)
+{
+    /* Prepare an array of f-p values. */
+    #define SIZE 5
+    double A[SIZE] = {1.,2.,3.,4.,5.};
+    /* Write array to a file. */
+    FILE * fp = fopen("test.bin", "wb");
+    fwrite(A,sizeof(double),SIZE,fp);
+    fclose (fp);
+ 
+    /* Read the f-p values into array B. */
+    double B[SIZE];
+    fp = fopen("test.bin","rb");
+    fpos_t pos;
+    if (fgetpos(fp,&pos) != 0)      /* 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 f-p value */
+    /* current position: after reading one f-p value */
+    printf("%.1f; read count = %d\n", B[0], ret_code);   /* print one f-p value and ret_code */
+ 
+    if (fsetpos(fp,&pos) != 0)   /* 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 f-p value */
+    printf("%.1f; read count = %d\n", B[0], ret_code);   /* print one f-p value and ret_code */
+    fclose(fp);
+ 
+    return EXIT_SUCCESS; 
+}

Output:

+
1.0; read count = 1
+1.0; read count = 1

References

See also

+ + + +
gets the file position indicator
(function)
returns the current file position indicator
(function)
moves the file position indicator to a specific location in a file
(function)
C++ documentation for fsetpos
+

+ © cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
+ https://en.cppreference.com/w/c/io/fsetpos +

+
-- cgit v1.2.3