fseek() in C
If we have multiple records inside a file and need to access a particular record that is at a specific position, so we need to loop through all the records before it to get the record. Doing this will waste a lot of memory and operational time. To reduce memory consumption and operational time we can use fseek() which provides an easier way to get to the required data. fseek() function in C seeks the cursor to the given record in the file.
Syntax for fseek()
int fseek(FILE *ptr, long int offset, int pos);
Example of fseek()
C
// C Program to demonstrate the use of fseek() in C #include <stdio.h> int main() { FILE * fp; fp = fopen ( "test.txt" , "r" ); // Moving pointer to end fseek (fp, 0, SEEK_END); // Printing position of pointer printf ( "%ld" , ftell (fp)); return 0; } |
Output
81
Basics of File Handling in C
File handing in C is the process in which we create, open, read, write, and close operations on a file. C language provides different functions such as fopen(), fwrite(), fread(), fseek(), fprintf(), etc. to perform input, output, and many different C file operations in our program.