fsetpos()(set file position) in C
In this article, we will discuss the difference between the fsetpos() function in C++ with its syntax and examples.
What is fsetpos() function?
The C programming language has a function called fsetpos() that can be used to set the file position indicator of a specific file stream. It is commonly utilized for random access file operations and is a component of the Standard I/O Library.
Syntax of fsetpos():
It has the following syntax:
int fsetpos(FILE *stream, const fpos_t *pos)
Parameters:
- stream: It is a reference to the stream's corresponding FILE object. Either a file opened with fopen() or one obtained from stdin, stdout, or stderr should be associated with it.
- pos: The file position indicator is represented by a pointer to an object of type fpos_t. This opaque data type is a representation of the file position and can store all the data required to restore the file position.
Return Type:
- The function fsetpos() returns zero if the operation is successful.
- In the event of an error, a non-zero value is returned.
Example 1:
Let's take an example to illustrate the fsetpos() function in C.
#include <stdio.h>
int main()
{
// File pointer
FILE* fp;
// Variable to store file position
fpos_t position;
// Open or create a file in write and read mode
fp = fopen("example.txt", "w+");
// Get current file position and store it
fgetpos(fp, &position);
// Write "Programming is fun!" to the file
fputs("Programming is fun!", fp);
// Set file position back to the stored position
fsetpos(fp, &position);
// Write "C programming" to the file from the restored position
fputs("C programming", fp);
// Setting file pointer position back to the stored position
fsetpos(fp, &position);
// Storing data stored in the file
char arr[50];
fgets(arr, 18, fp);
// Printing output
puts(arr);
// Close the file
fclose(fp);
return 0;
}
Output:
C programmings fu
Explanation:
- The first line of the file says, "Programming is fun!"
- fgetpos() is used to store the file position.
- The file is written with "C programming" from the position that was restored. Part of the old content is replaced by the new content because it is shorter than the original.
- The stored position becomes the new file position.
- fgets() is used to read the first 18 characters (including the overwritten portion).
- The read content "Programming is fu" is printed using the puts() function.
Example 2:
Let's take another example to illustrate the fsetpos() function in C.
#include <stdio.h>
int main()
{
// File pointer
FILE* fp;
// Variable to store file position
fpos_t position;
// Open or create a file in write and read mode
fp = fopen("example.txt", "w+");
// Get current file position and store it
fgetpos(fp, &position);
// Write "OpenAI is amazing!" to the file
fputs("JAVA is amazing!", fp);
// Set file position back to the stored position
fsetpos(fp, &position);
// Write "ChatGPT is powerful" to the file from the restored position
fputs("Javatpoint is application", fp);
// Setting file pointer position back to the stored position
fsetpos(fp, &position);
// Storing data stored in the file
char arr[50];
fgets(arr, 28, fp);
// Printing output
puts(arr);
// Close the file
fclose(fp);
return 0;
}
Output:
Javatpoint is application
Example 3:
Let's take another example to illustrate the fsetpos() function in C.
#include <stdio.h>
int main()
{
// File pointer
FILE* fp;
// Variable to store file position
fpos_t position;
// Open or create a file in binary write and read mode
fp = fopen("numbers.bin", "wb+");
if (fp == NULL) {
perror("Error opening file");
return 1;
}
// Write integers to the file
int numbers[] = {42, 123, 789, 456, 999};
fwrite(numbers, sizeof(int), sizeof(numbers) / sizeof(int), fp);
// Get current file position and store it
fgetpos(fp, &position);
// Set file position back to the stored position
fsetpos(fp, &position);
// Read integers from the file
int readNumbers[5];
fread(readNumbers, sizeof(int), sizeof(readNumbers) / sizeof(int), fp);
// Print the read integers
printf("Read numbers from the file: ");
for (int i = 0; i < sizeof(readNumbers) / sizeof(int); ++i) {
printf("%d ", readNumbers[i]);
}
printf("\n");
// Close the file
fclose(fp);
return 0;
}
Output:
Read numbers from the file: 4543 0 640 0 0
Explanation:
In conclusion, an effective method for modifying the file position indicator inside a file stream is the C function fsetpos(). Programmers can set the location within a file using this function, which enables efficient navigation and random access while operating on files. By accepting an fpos_t object and a FILE pointer as inputs, the function allows fgetpos() to be used to restore a previously saved file position. When it comes to situations where you need to read or write data at specific locations within a file, this feature comes in handy. Fsetpos() improves the flexibility and control that programmers have over file operations in the C programming language, whether it is for updating particular sections of a file, creating custom file handling procedures, or managing intricate file structures.