Open In App

fseek() vs rewind() in C

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In C, the functions fseek() and rewind() helps in manipulating the position of the pointer in the opened file but serve different purposes and have distinct working. The below table lists the major differences between fseek() and rewind() in C:

Featurefseek()rewind()
FunctionalityMoves the file pointer to a specific location.Resets the file pointer to the beginning of the file.
ArgumentsTakes three arguments: file pointer, offset, and whence.Takes only the file pointer.
FlexibilityMore flexible as it allows moving the file pointer relative to the beginning, current position, or end.Less flexible, only resets the pointer to the beginning.
Error CheckingReturns an integer (0 for success, non-zero for failure).Does not return anything (does not provide error status directly).
Use CaseUsed when you need to move the pointer to a specific location (e.g., for random access).Used for resetting the file pointer (e.g., for reading the file from the beginning).

Which should be preferred?

In C, fseek() should be preferred over rewind() even if we want to move to the beginning of the file. It is due to the better error handling by fseek() as compared to the rewind() function.

Let's take a look at the signature of each function:

Syntax of fseek()

int fseek(FILE *stream, long offset, int whence);

It returns 0 when successful.

Syntax of rewind()

void rewind(FILE *stream);

As the rewind() function sets the file position indicator for the stream pointed to by the stream at the beginning of the file. It is also equivalent to

(void) fseek(stream, 0L, SEEK_SET);

except that the error indicator for the stream is also cleared, and it does not return any value making it harder to find whether the function was successful or not. This following code example sets the file pointer of an input stream back to the beginning using rewind(). But there is no way to check whether the rewind() was successful.

C
#include <stdio.h>
#include <stdlib.h>

int main() {
    FILE* fptr = fopen("test.txt", "r");

    if (fp == NULL) {
        // Handle open error
    }

    // Do some processing with file

    // no way to check if rewind is successful
    rewind(fptr);

    // Do some more precessing with file
    return 0;
}


In the above code, fseek() can be used instead of rewind() to see if the operation succeeded.

The following lines of code can be used in place of rewind(fp);

C
if ( fseek(fp, 0L, SEEK_SET) != 0 ) {
	// Handle repositioning error
}

Similar Reads