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.

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:

Picture 1 of The function fgetpos () in C

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:

Picture 2 of The function fgetpos () in C

According to Tutorialspoint

Previous lesson: Function fflush () in C

Next lesson: Function fopen () in C

You've just finished reading the article "The function fgetpos () in C" edited by the TipsMake team. You can save the-function-fgetpos-in-c.pdf to your computer here to read later or print it out. We hope this article has provided you with many useful tech tips and tricks. You can search for similar articles on tips and guides. Thank you for reading and for following us regularly.

« PREV : Function fopen () in...
Function fflush ()... : NEXT »