What Is EOF (End Of File)? Examples with PHP, C++, C, Python, Java – POFTUT

What Is EOF (End Of File)? Examples with PHP, C++, C, Python, Java

Files contain different types of data like text, image, video, headers, graphics, etc. All these data are stored in different encoding and formatting techniques but every file has an end which is named End Of File which sets the last by of the given file. In this tutorial, we will learn the meaning of the End Of File and relation with the popular programming languages like C, C++, PHP, Java, Python.

What Is End Of File?

End Of File is the special data or delimiter which will set the end of file for a specific file. This file contains different types of data from text to image but the end of the file is the same for all of them. End Of File can be also expressed as EOF in a short form. EOF is also used in different programming languages in order to express and check the end of the file.

Checking the end of the file is important especially developing applications. While reading a file to process, print or view we need to check the end of file in some cases especially in low-level operations.

End Of File In C and C++

C and C++ provide different file operation functions. We can use EOF value in order to check the end of the file which can be used to check different functions return value. EOF stores the -1 where a file operation function will return -1 when the end of file is reached.

In the following example, we will read the file named myfile.txt with the getc() function which will read a single character from a given file for each time. We will check the EOF after every read operation.

#include <stdio.h> 

int main() 
   FILE *fp = fopen("myfile.txt", "r"); 
   int ch = getc(fp);

   //Check enf of file and if not end execute while block
   //File EOF reached end while loop 
   while (ch != EOF) 
    /* Print the file content with ch */
        /* Read one character from file */
    ch = getc(fp); 
   if (feof(fp)) 
    printf("\n End of file reached."); 
    printf("\n Something went wrong."); 

   return 0; 

End Of File In PHP

PHP provides feof() function in order to check the end of the file. When there are some bytes or not end of file the feof() function will return false and the provided iteration will continue till to end of the file.


// We will open the myfile.txt and set to variable $check 
$check = fopen("myfile.txt", "r"); 

$seq = fgets($check); 

// Outputs a line of the file until 
// the end-of-file is reached 
while(! feof($check)) 
echo $seq ; 
$seq = fgets($check); 

// We will close the file with fclose() function 


End Of File In Java

Java programming language provides different functions in order to read, write files. In Java when a file is read the value generally stored in a variable like a String. If the end of the file is reached the returned value will be a null which is simply nothing. We can check the end of the file if the returned value is null like below.

import java.io.*;
import java.util.*;

public class End_Of_File_Example{

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String ab= scanner.nextLine();
        int a=0;

        while(ab != null){
            System.out.printf("%d %s\n",++a,ab);

End Of File In Python

In Python, there is no specific EOF function but we can use some techniques like checking line we read and determine the EOF. We will read the file line by line with while loop. If the end of the file is reached the returned line value will be null.

# We will set the file name we want to read
filename = "myfile.txt" 

# We open file with open() function to only read
filehandle= open(filename, 'r') 

while True: 
   #Read single line   
   line = filehandle.readline() 
   #Check line if it is not null
   #If line is null this means EOF
   if not line: 
      break print(line) 

# Close the file handler 

LEARN MORE  C fgets() Function Usage Examples To Read File

Leave a Comment