Definition of C++ Read File
As we are well aware that C++ is one of the most widely used languages and it provides many features that allows the programmer to perform multiple tasks easily. In C++, working on file operations is as easy as working normally on the console operations using the cin and cout. Practically working with files is very commonly used in order to store the data permanently. fstream is the C++ library which is used to perform read and write file operations like iostream. The stream is nothing but used as the destination to inpt or output the data from one location to another. There are several datatypes of this library which perform specific tasks of file operations. ‘ifstream’ is one of the data types used specifically for reading files in C++.
How to Read File in C++?
As mentioned above, ‘ifstream’ data type of ‘fstream’ library is used to read the files of C++. But before reading, there are several tasks which are performed sequentially like opening the file, reading and closing it. Different data types are used for the specific purpose. Let’s understand the datatypes of ‘ifstream’ mentioned below:
Data Type Name | Data Type Description |
ofstream | This data type is used to open the file or write something in the file. It represents the output file stream. |
ifstream | This data type is used to read the data from the file. It represents the input file stream. |
fstream | This data type represents a normal file stream and can perform the tasks of both input and output file streams. It can be used to open, create, read and write in the file. |
Given below is the step by step procedure to the file content in C++ :
1. Opening the Already Created File
In order to read the information from the file, we need to first open it. The opening of the file is done using ofstream or fstream object of the file. Files in C++ can be opened in different modes depending on the purpose of writing or reading. Hence, we need to specify the mode of file opening along with the file name.
There are basically 3 default modes which are used while opening a file in C++:
- ofstreamios: : out
- fstreamios: : in | ios: : out
- ofstreamios : :out
Syntax:
void open(filename, ios: : openmodemode_name);
2. Read the Information from The File
We can simply read the information from the file using the operator ( >> ) with the name of the file. We need to use the fstream or ifstream object in C++ in order to read the file. Reading of the file line by line can be done by simply using the while loop along with the function of ifstream ‘getline()’.
3. Close the File
As we all know about the C++ memory management, as the program terminates, it frees all the allocated memory and the used resources. But still it is considered to be a good practice to close the file after the desired operation is performed.
Syntax:
void close();
Examples of C++ Read File
Below given are the few examples along with their outputs to demonstrate how the file read operation is performed in C++ :
Example #1
Code:
#include <iostream>
#include <fstream>
using namespace std;
intmain(){
char data[100];
// creating the file variable of the fstream data type for writing
fstreamwfile;
// opening the file in both read and write mode
wfile.open ("demo.txt", ios::out| ios::in );
// Asking the user to enter the content
cout<< "Please write the content in the file." <<endl;
// Taking the data using getline() function
cin.getline(data, 100);
// Writing the above content in the file named 'demp.txt'
wfile<< data <<endl;
// closing the file after writing
wfile.close();
//creating new file variable of data type 'ifstream' for reading
ifstreamrfile;
// opening the file for reading the content
rfile.open ("demo.txt", ios::in| ios::out );
// Reading the content from the file
rfile>> data;
cout<< data <<endl;
//closing the file after reading is done
rfile.close();
return 0;
}
Output:
Explanation: In the above example, we have created two file variables of ‘fstream’ and ‘ifstream’ data type for writing and reading of the file respectively. In order to read or write the file, we need to first open the file using the open() function and define its mode. After opening, writing of the content in the file is done through the ( << ) operator and the file is closed after writing using the close() function. Now the file is opened again in order to read its content (using >> operator) and display it on the console (using cout function). In order to release all the resources and free the allocated memory close() function is used.
Example #2
When the file user is reading is not found.
Code:
#include<iostream>
#include<fstream>
using namespace std;
intmain()
{
char ch;
//creating the object of the data type 'istream'
ifstreamnew_file;
//opening the above file
new_file.open("demo1.txt",ios::in);
//checking whether the file is available or not
if(!new_file)
{
cout<<"Sorry the file you are looking for is not available"<<endl;
return -1;
}
// reading the whole file till the end
while (!new_file.eof())
{
new_file>>noskipws>>ch;
// printing the content on the console
cout<<ch;
}
//closing the file after reading
new_file.close();
return 0;
}
Output:
Explanation: In the above code, we are creating the object of the data type ‘ifstream’ in order to read the file named ‘demo1.txt’. The file is opened using the open() function in read mode using ‘ios::in’. We have used the if and else statement to check whether the file is [resent or not. If the file is not found, a proper message is displayed on the console. Otherwise, it will read the whole file using the ‘>>’ operator, and the content is printed on the console. In order to release all the resources and free the memory, the close() function is used.
Conclusion
The above description clearly explains how the reading of files is done in C++ and the various data types of file stream used for specific operations. As working with files is very common in the practical world, it is very important to understand each and every file operation deeply before using them.
Recommended Articles
This is a guide to C++ Read File. Here we also discuss the definition and how to read file in c++? along with different examples and its code implementation. You may also have a look at the following articles to learn more –
4 Online Courses | 5 Hands-on Projects | 37+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses