The function fgetpos () in C
The int fgetpos function (FILE * stream, fpos_t * pos) in the C Library standardizes the position of the current file of Stream and writes it to pos.
Declare the function fgetpos () in C
Below is the declaration for the fgetpos () function in C:
int fgetpos ( FILE * stream , fpos_t * pos )
Parameters
stream - This is the pointer to a FILE object that identifies the Stream.
pos - This is the pointer to an fpos_t object.
Returns the value
This function returns 0 if successful, and returns a value other than 0 if there is an error.
For example
The following C program illustrates the usage of the fgetpos () function in C:
#include int main () { FILE * fp ; fpos_t position ; fp = fopen ( "baitapc.txt" , "w+" ); fgetpos ( fp , & position ); fputs ( "Hello, World!" , fp ); fsetpos ( fp , & position ); fputs ( "Noi dung nay se ghi de phan noi dung da co truoc trong file" , fp ); fclose ( fp ); return ( 0 ); }
Compile and run the above program to create a bait file.txt with the following content. First we take the original location of the file using the fgetpost () function and then we write Hello, World! in this file, then we use the fsetpos () function to restore the cursor to the beginning of the file and then overwrite the file with the following content:
Now you follow the contents of the above file by using the following C program:
#include int main () { FILE * fp ; int c ; int n = 0 ; fp = fopen ( "baitapc.txt" , "r" ); while ( 1 ) { c = fgetc ( fp ); if ( feof ( fp ) ) { break ; } printf ( "%c" , c ); } fclose ( fp ); return ( 0 ); }
Compiling and running the above C program will result:
According to Tutorialspoint
Previous lesson: Function fflush () in C
Next lesson: Function fopen () in C
You should read it
Maybe you are interested
How to turn off file groups in Downloads Windows 11
How to Delete Recent Files on Windows 11
How to clean junk files when drive C on the computer is full
Crack Excel password, remove Sheet password in Excel file
How to cut MP3 music without using software, export MP3 file
Gemini can now recognize PDF files appearing on smartphone screens