Home > Backend Development > C++ > In C/C++, the fseek() function is used to move the position of the file pointer in the file.

In C/C++, the fseek() function is used to move the position of the file pointer in the file.

PHPz
Release: 2023-09-02 15:57:13
forward
1402 people have browsed it

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)
Copy after login

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!
Copy after login

Now let's look at the code.

Example

#include<stdio.h>
void main() {
   FILE *f;
   f = fopen("demo.txt", "r");
   if(f == NULL) {
      printf("\n Can&#39;t open file or file doesn&#39;t exist.");
      exit(0);
   }
   fseek(f, 0, SEEK_END);
   printf("The size of file : %ld bytes", ftell(f));
   getch();
}
Copy after login

Output

The size of file : 78 bytes
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template