Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

  1. Home
  2. C Language Dictionary
  3. fseek() / ftell() / rewind() / feof()

fseek() / ftell() / rewind() / feof()

Functions for moving and retrieving the current file position (file position indicator), and for checking whether the end of a file has been reached. Use these for file processing that requires random access.

Syntax

// Moves the file position by offset bytes from the reference point (whence).
// Returns: 0 on success, nonzero on failure.
int fseek(FILE *stream, long offset, int whence);

// Returns the current file position in bytes.
// Returns: current position, or -1L on error.
long ftell(FILE *stream);

// Rewinds the file position to the beginning (also clears the error flag).
void rewind(FILE *stream);

// Checks whether the end-of-file flag is set.
// Returns: nonzero if EOF has been reached, 0 otherwise.
int feof(FILE *stream);

whence (reference point) values

ConstantReference pointDescription
SEEK_SETBeginning of fileMoves to offset bytes from the beginning. offset must be 0 or greater.
SEEK_CURCurrent positionMoves offset bytes forward or backward from the current position. A negative value moves backward.
SEEK_ENDEnd of fileMoves offset bytes from the end of the file. To get the file size, pass 0 as offset.

Sample code

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

int main(void) {
    // Get the file size using fseek + ftell.
    FILE *fp = fopen("lines.txt", "rb");
    if (fp == NULL) { perror("fopen"); return EXIT_FAILURE; }

    fseek(fp, 0, SEEK_END);     // Move to the end of the file.
    long file_size = ftell(fp); // Current position equals the file size in bytes.
    printf("File size: %ld bytes\n", file_size);

    rewind(fp); // Return to the beginning.
    printf("Position after rewind: %ld\n", ftell(fp)); // Prints "0".

    // Move to byte 7 from the beginning with SEEK_SET and read from there.
    fseek(fp, 7, SEEK_SET);
    char buf[64];
    if (fgets(buf, sizeof(buf), fp) != NULL) {
        printf("From byte 7: %s", buf); // Prints the content of the second line.
    }

    // Use feof to confirm the loop ended at EOF (recommended: checking the return value is more reliable).
    rewind(fp);
    int ch;
    int count = 0;
    while ((ch = fgetc(fp)) != EOF) {
        count++;
    }
    if (feof(fp)) {
        printf("Reached end of file normally. Character count: %d\n", count);
    }

    fclose(fp);
    return 0;
}

Notes

Calling fseek() on a file opened in text mode with any combination other than SEEK_SET and a value returned by ftell() produces implementation-defined behavior. Always open the file in binary mode ("rb") when random access is needed.

feof() only checks whether the end-of-file flag has been set. Using while (!feof(fp)) as a loop condition can cause the last piece of data to be processed twice. It is recommended to check for EOF using the return value of fgets() or fgetc() instead.

For general file read/write functions, see also 'fread() / fwrite()' and 'fgets() / fputs()'.

If you find any errors or copyright issues, please .