feof_unlocked - Linux


feof_unlocked checks end-of-file indicator for a stream. It is used to test the end-of-file indicator associated with a stream. This indicator is set when the end-of-file is encountered on a read operation.


 int feof_unlocked(FILE *stream);




Check for end-of-file on a file:

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

int main() {
  FILE *fp = fopen("myfile.txt", "r");
  if (fp == NULL) {
    perror("Error opening file");

  while (!feof_unlocked(fp)) {
    // Read and process data from the file

  return 0;

Common Issues

  • Incorrect stream: Ensure that the stream pointer passed to feof_unlocked is valid and points to an open file.
  • Unexpected end-of-file: Check for unexpected end-of-file by examining the value of feof_unlocked after every read operation.


feof_unlocked can be integrated with other file-handling functions such as fread, fwrite, and fseek to control file reading and writing operations.

Related Commands

  • feof: Similar to feof_unlocked, but is not thread-safe.
  • ferror: Checks for file errors.
  • fread: Reads data from a stream.
  • fwrite: Writes data to a stream.
  • fseek: Moves the file pointer to a specific position in the stream.