fgetpos() Function
The fgetpos()
function in C is used to retrieve the current position in a stream. It stores the necessary information in a fpos_t
object that can later be used with fsetpos()
to restore the stream’s position and, if applicable, its multibyte state. This function is useful for saving the current file position before performing operations that may change it.
Syntax of fgetpos()
int fgetpos(FILE *stream, fpos_t *pos);
Parameters
Parameter | Description |
---|---|
stream | Pointer to a FILE object that identifies the stream. |
pos | Pointer to a fpos_t object where the current position will be stored. This object must be allocated before the call. |
It is important to note that fgetpos()
not only saves the current file position but also any additional state required to fully restore the stream later with fsetpos()
. For an alternative that returns the position as an integer value, consider using ftell()
.
Return Value
On success, fgetpos()
returns zero. In the case of an error, a non-zero value is returned and errno
is set to a platform-specific positive error code.
Examples for fgetpos()
Example 1: Basic Usage of fgetpos() to Save and Restore File Position
This example demonstrates how to use fgetpos()
to save the current position in a file and then restore it later using fsetpos()
.
Program
#include <stdio.h>
int main() {
FILE *fp = fopen("example.txt", "r");
if (fp == NULL) {
perror("Error opening file");
return 1;
}
fpos_t pos;
// Save the current file position
if (fgetpos(fp, &pos) != 0) {
perror("Error getting file position");
fclose(fp);
return 1;
}
// Read and print the first character
int ch = fgetc(fp);
printf("First character: %c\n", ch);
// Restore the file position
if (fsetpos(fp, &pos) != 0) {
perror("Error setting file position");
fclose(fp);
return 1;
}
// Read and print the first character again
ch = fgetc(fp);
printf("First character after restoring position: %c\n", ch);
fclose(fp);
return 0;
}
Explanation:
- The file
example.txt
is opened in read mode. fgetpos()
is used to store the current file position in the variablepos
.- The first character is read and printed.
fsetpos()
is called to restore the file position to the saved value.- The first character is read again to confirm that the position was restored.
Program Output:
First character: X
First character after restoring position: X
Example 2: Handling Errors When Using fgetpos()
This example illustrates error handling when fgetpos()
fails to retrieve the file position.
Program
#include <stdio.h>
int main() {
FILE *fp = fopen("nonexistent.txt", "r");
if (fp == NULL) {
perror("Error opening file");
return 1;
}
fpos_t pos;
// Attempt to get file position; expected to fail if file is not accessible
if (fgetpos(fp, &pos) != 0) {
perror("Error getting file position");
} else {
printf("File position retrieved successfully.\n");
}
fclose(fp);
return 0;
}
Explanation:
- An attempt is made to open a file named
nonexistent.txt
, which does not exist. - The failure of
fopen()
is handled by printing an error message usingperror()
. - If the file were to open,
fgetpos()
would be called to retrieve the current file position, and error handling would be performed based on its return value.
Program Output:
Error opening file: No such file or directory
Example 3: Using fgetpos() in a File Copy Operation
This example demonstrates how fgetpos()
can be used to mark a position in a file before reading data, which can be useful in file copy or seek operations.
Program
#include <stdio.h>
int main() {
FILE *source = fopen("source.txt", "r");
FILE *dest = fopen("dest.txt", "w");
if (source == NULL || dest == NULL) {
perror("Error opening file");
return 1;
}
fpos_t pos;
// Mark the current position in the source file
if (fgetpos(source, &pos) != 0) {
perror("Error getting file position");
fclose(source);
fclose(dest);
return 1;
}
// Copy the file content
int ch;
while ((ch = fgetc(source)) != EOF) {
fputc(ch, dest);
}
// Restore the file position to the marked point
if (fsetpos(source, &pos) != 0) {
perror("Error setting file position");
} else {
printf("File position restored successfully.\n");
}
fclose(source);
fclose(dest);
return 0;
}
Explanation:
- The source file
source.txt
is opened for reading and the destination filedest.txt
for writing. fgetpos()
is used to mark the current position in the source file.- The file content is copied from the source to the destination.
- After copying,
fsetpos()
restores the source file’s position to the marked point. - A message is printed to confirm that the file position was successfully restored.
Program Output:
File position restored successfully.