fseek and ftell in c
Mastering fseek and ftell in C for File Handling
In C, the functions fseek()
and ftell()
are used to move the file pointer to a specified position and to determine the current position of the file pointer, respectively. These functions are typically used to manipulate the position of the file pointer when reading from or writing to a file.
What is fseek function In c?
The fseek()
function takes three arguments: a pointer to a FILE
object, an offset from the specified origin, and a position indicator. The origin can be one of three values: SEEK_SET, SEEK_CUR
, or SEEK_END. SEEK_SET
sets the file position indicator to the beginning of the file, SEEK_CUR
sets the position indicator to the current position, and SEEK_END
sets the position indicator to the end of the file. The offset is added to the position indicator to determine the final position.
Example of using fseek()
to move the file pointer to the end of a file:
FILE *fp;
long pos;
fp = fopen("myfile.txt", "r");
fseek(fp, 0, SEEK_END); // move the file pointer to the end of the file
pos = ftell(fp); // get the current position of the file pointer
fclose(fp);
What is Ftell in c?
The ftell()
function returns the current value of the file position indicator for the specified FILE
object. The return value is a long int
representing the number of bytes from the beginning of the file to the current position.
Example of using ftell()
to determine the current position of the file pointer:
FILE *fp;
long pos;
fp = fopen("myfile.txt", "r");
pos = ftell(fp); // get the current position of the file pointer
fclose(fp);
Further Reading:
fseek and ftell in c
fread() and fwrite() in C
Enroll Now:
[ C-Programming From Scratch to Advanced 2023-2024] "Start Supercharging Your Productivity!"
Contact Us:
- For any inquiries, please email us at [[email protected]].
- Follow us on insta [ electro4u_offical_ ] for updates and tips.
Note: If you encounter any issues or specific errors when running this program, please let me know and I'll be happy to help debug them!