How to Read a File Character by Character in C++? Last Updated : 06 Mar, 2024 Comments Improve Suggest changes Like Article Like Report In C++, file handling is used to store data permanently in a computer. Using file handling we can store our data in secondary memory (Hard disk). In this article, we will learn how to read a file character by character in C++. Example: Input: "Geeks for Geeks"Output: G e e k s f o r G e e k sRead Text from a File Character by Character in C++To read a file character by character in C++, we can make use of the ifstream class to create an input stream from the file and then use the ifstream::get() function which sets the given variable to the current character read from a file. ApproachWe will declare a variable called file with the help of ifstream class and pass the path of the file to it.Now first we will check if the file got read or not.If not then we will print an error while opening the file.If the file is opened successfully then we will iterate over the file till the ifstream::get() function returns NULL.While iterating, we will print each character one by one.C++ Program to Read a File Character by Character C++ // C++ Program to illustrate how to read a file character by // character #include <fstream> #include <iostream> using namespace std; int main() { // Open the file ifstream file("example.txt"); // Check if the file was opened successfully if (!file) { cout << "Unable to open file"; return 1; } // Read the file character by character char ch; while (file.get(ch)) { cout << ch << " "; } cout << endl; // Close the file file.close(); return 0; } Output G e e k s f o r G e e k s Time complexity: O(N)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article How to Read a File Character by Character in C++? B bhushanc2003 Follow Improve Article Tags : C++ Programs C++ cpp-string cpp-file-handling CPP Examples +1 More Practice Tags : CPP Similar Reads How to Extract a Substring from a Character Array in C++? In C++, character arrays are used to store sequences of characters also known as strings. A substring is a part of a string that consists of a continuous sequence of characters from the string. In this article, we will learn how to extract a substring from a character array in C++. Extract a Substri 2 min read How Do I Handle Multi-Byte Characters in C++? In C++, characters are represented using the char data type, which is designed to hold a single byte. However, there are many languages other than English, and all these characters cannot be represented by a single byte. These characters are known as multi-byte characters. In this article, we will l 2 min read How to Read Data from a CSV File to a 2D Array in C++? A comma-separated value file also known as a CSV file is a file format generally used to store tabular data in plain text format separated by commas. In this article, we will learn how we can read data from a CSV file and store it in a 2D array in C++. Example: Input: CSV File = âdata.csvâ //contain 3 min read How to Read a File Line by Line in C++? In C++, we can read the data of the file for different purposes such as processing text-based data, configuration files, or log files. In this article, we'll learn how to read a file line by line in C++. Read a File Line by Line in C++We can use the std::getline() function to read the input line by 2 min read How to Read From a File in C++? Reading from a file means retrieving the data stored inside a file. C++ file handling allows us to read different files from our C programs. This data can be taken as input and stored in the program for processing. Generally, files can be classified in two types:Text File: Files that contains data i 4 min read Like