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%2Ffgetpos.html | 58 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 devdocs/c/io%2Ffgetpos.html (limited to 'devdocs/c/io%2Ffgetpos.html') diff --git a/devdocs/c/io%2Ffgetpos.html b/devdocs/c/io%2Ffgetpos.html new file mode 100644 index 00000000..4a403e25 --- /dev/null +++ b/devdocs/c/io%2Ffgetpos.html @@ -0,0 +1,58 @@ +

fgetpos

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 stream stream and stores them in the object pointed to by pos. The value stored is only meaningful as the input to fsetpos.

+

Parameters

+ + +
stream - file stream to examine
pos - pointer to a fpos_t object to store the file position indicator to

Return value

​0​ upon success, nonzero value otherwise.

+

Example

#include <stdio.h>
+#include <stdlib.h>
+#include <assert.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");
+}

Output:

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

References

See also

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

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

+
-- cgit v1.2.3