The purpose of this delimiter is to indicate the end of file to file-reading programs. This is also useful for the storage and transmission of data.
The following code detects the EOF in C++. It reads and displays each character in the file until it reaches the end of file delimiter. Once EOF is detected, it outputs it as well.
#include <iostream>#include <fstream>int main () {std::ifstream is("myfile.txt"); // open filechar c;while (is.get(c))std::cout << c;if (is.eof())std::cout << "[EoF reached]\n";elsestd::cout << "[error reading]\n";is.close();return 0;}
The following code detects the EOF in Python. It reads and displays each character in the file until it reaches the end of file delimiter. Once EOF is detected, it outputs it as well.
f = open("myfile.txt", "r")while True:line = f.readline()if not line:print("EoF reached")breakprint(line)