Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
fseek()
2.1.
Time for an Example
3.
rewind()
3.1.
Implementation 
4.
fseek() vs rewind() in C
5.
Frequently Asked Questions
5.1.
What is file handling?
5.2.
What is the fopen() function?
5.3.
What is the ftell() function?
6.
Conclusion
Last Updated: Mar 27, 2024

fseek() vs rewind() in C

Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

fseek() and rewind()  are the functions we use in file handling to manipulate the file pointer. There are some differences in both of them, and we will learn about those differences so that we can use both functions effectively.

So, let’s get started: 

Recommended Topics, Sum of Digits in C and C Static Function.

fseek()

fseek() is the function that sets the file pointer at a specific position like at the beginning , end or at current position. fseek() consists of three parameters: 

  • pointer to file
  • offset
  • whence 

It returns zero if successful, else non-zero. 

Syntax

Below are the parameters explained:

  • pointer - pointer to the file we are working on.
  • Offset- it sets the number of bytes from the file pointer to read
  • Whence- whence sets the position of the pointer. Whence parameters is further divided into three categories: 
    SEEK_SET- sets the pointer at the beginning,
    SEEK_END-sets the pointer at the end,
    SEEK_CUR-sets the pointer to the current position of file pointer

Below is the source code to explain the working of fseek():

Time for an Example

#include <stdio.h>
#include<stdlib.h>
int main()
{
    FILE *ptr;
    /*reading the file using fopen method*/
    ptr = fopen("test.txt","r"); 
     if(ptr==NULL)
     {
         printf("file does not exist or file is invalid");
         exit(0);
     }
     /*printing the current position of pointer*/
     printf("current position of file pointer: %ld \n", ftell(ptr)); 
     
     int result = fseek(ptr,5,SEEK_SET);
     
     /*printing the position of pointer after applying fseek()*/
     printf("position of file pointer after fseek: %ld \n", ftell(ptr)); 
     
     if(result==0)
     {
         printf("fseek was successful");
     }
         return 0;
}

 

In the above code we check the position of the pointer before or after the fseek() function.

Output of the above code:

From the output we can evaluate that we can use the fseek() function to change the position of the pointer and then we can read the file from that particular position only.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

rewind()

rewind() is the function that we can use to set the position of a file pointer to the beginning of a file and it does not return any value like fseek().

 

Syntax

Implementation 

#include <stdio.h>
#include<stdlib.h>
int main()
{
    FILE *ptr;
    /*reading the file using fopen method*/
    ptr = fopen("test.txt","r"); 
     if(ptr==NULL)
     {
         printf("file does not exist or file is invalid");
         exit(0);
     }
      /*printing the current position of pointer*/
     printf("current position of file pointer: %ld \n", ftell(ptr));
     
     rewind(ptr);
     /*printing the position of pointer after applying rewind()*/
     printf("position of file pointer after rewind: %ld \n", ftell(ptr)); 
     
    return 0;
}

 

Output

fseek() vs rewind() in C

  • The first difference between fseek() and rewind() is that fseek() can return a value so that we can tell whether the operation succeeded or not.
  • The second difference is that rewind() only sets the position of the file pointer only at the beginning.

You can also read about dynamic array in c, and  Tribonacci Series

Must Read what is storage class in c and Decision Making in C

Frequently Asked Questions

What is file handling?

It is the process of storing data in a file with the help of a program. The programs in the C programming language use file handling to save the program's results and other data to a file. We can also extract/fetch data from a file to use in the program.

What is the fopen() function?

It is the function that we use to create a file or to read an existing file in the system.

What is the ftell() function?

It is the function that tells us about the current position of the file pointer in the console.

Conclusion

In this article, we learned about the use of fseek() and rewind() functions in C language and the main difference between both functions.

To learn more about the file handling in c or c programming, please refer to the following article:

Ternary Operator in C

Short int in C Programming

Introduction to functions,

Introduction to file handling

Pointers in c

Getch in C

Bubble Sort Program in C

Conditional Operator in C

To learn more about  DSA,competitive coding and many more knowledgeable topics please look into the guided paths on Coding Ninjas Studio. Also you can enroll into our courses and check out the mock test and problems available to you. Please check out our  interview experiences and interview bundle for placement preparations.

Please upvote our blog to help other ninjas grow.

Happy Learning

Previous article
C ftell() function
Next article
fgets() and gets() in C
Live masterclass