Home >Backend Development >C++ >In C/C++, the fseek() function is used to move the position of the file pointer in the file.
fseek() is used in C language to move the file pointer to a specific location. Offsets and streams are the targets of pointers, and they are given in function parameters. If successful, it returns zero. If unsuccessful, it returns a non-zero value.
The following is the syntax of fseek() in C language:
int fseek(FILE *stream, long int offset, int whence)
Here are the parameters used in fseek():
stream − This is a pointer used to identify the stream.
offset − This is the number of bytes starting from the position.
whence − This is where the offset is added.
whence is specified by one of the following constants.
SEEK_END − End of file.
SEEK_SET − Beginning of file.
SEEK_CUR − The current position of the file pointer.
This is an example of fseek() in C language.
Suppose we have a file called "demo.txt" with the following content:
This is demo text! This is demo text! This is demo text! This is demo text!
Now let's look at the code.
#include<stdio.h> void main() { FILE *f; f = fopen("demo.txt", "r"); if(f == NULL) { printf("\n Can't open file or file doesn't exist."); exit(0); } fseek(f, 0, SEEK_END); printf("The size of file : %ld bytes", ftell(f)); getch(); }
The size of file : 78 bytes
The above is the detailed content of In C/C++, the fseek() function is used to move the position of the file pointer in the file.. For more information, please follow other related articles on the PHP Chinese website!