Updated April 3, 2023
Introduction to ifstream in C++
When you code, sometimes you need to read some file to process the code to the next phase and for that, we need something in our code that can help us in reading the required file from any location. This is also known as file handling and for that, we need stream classes and it is done by using fstream, ofstream, and ifstream classes. Ifstream is an input stream for files and with it, we can read any information available in the file. For using these stream classes we need to add <iostream> and <fstream> header files in your code.
Syntax
Now let us have a look at the syntax of ifstream classes:
ifstreamobject_name( "file_name " ) ;
Ifstream object is used for reading the file ofstream object is used to write the file in your code.
Different Types of File Modes
As we have seen what ifstream is and how can we use it in our code for performing various operations in code, it can be reading a file, writing to a file or maybe accessing a file, In addition, we will see the working of ifstream through some C ++ code examples. But before moving to the code we will see the different types of file modes can be used in our source code through the below table:
Input-Output Stream Prefix | Description |
ios::in | This ios prefix is used to open a file to read input from the user. |
ios::out | This ios prefix is used to open a file to write the output from the user. |
ios::ate | This ios prefix is used to open a file without truncating and allows data to be written anywhere in the mentioned file. |
ios::trunc | This ios prefix is used to truncate the existing file. |
ios::app | This ios prefix is used to open a file and append it to the end. |
ios::binary | This ios prefix is used to treat the given file as a binary format. |
Examples to Implement ifstream in C++
Below are the examples:
Example #1
Here is the C ++ code to demonstrate the working of the stream for writing into a file in programming.
Code:
#include <iostream>
#include <fstream>
using namespace std ;
int main () {
ofstreampersonal_file ;
personal_file.open ("file.txt") ;
personal_file<< " Writing this to a file.\n " ;
personal_file.close() ;
return 0 ;
}
Output:
Explanation: In the above code, we are creating a file with the name file and then we are creating an ofstream object so that we can write into the file after opening it through the file. open syntax. Finally, we closed the file after writing the information into it.
Example #2
Here is the C ++ code to demonstrate the working of ifstream classes:
Code:
#include <iostream>
#include <string>
#include <fstream>
using namespace std ;
int main() {
// Writting to a file
ofstreampersonal_file ; // outs is an output stream of iostream class
personal_file.open("Demo.txt") ; // connect outs to file outFile
personal_file<< "HEY! Welcome to C ++ File handling.\n";
personal_file<< "Start learning file handling.\n";
personal_file.close () ; // closing the output file stream
// Reading from a file
string line ;
ifstreampersonal_filein ; // here the personal_filein is an input stream
personal_filein.open ("Demo.txt") ; // connecting personal_filein to file Input File
if ( personal_filein.is_open () )
{
while ( getline ( personal_filein , line ) )
{
cout<< line << '\n';
}
personal_file.close () ; // closing the output file stream
}
return 0 ;
}
Output:
Explanation: As you can see in the above code we have used fstream in our header files to include all the file and stream classes. As we are using Ustream to handle the output stream. Through a file. open we are opening a file name ” Demo.txt”. After opening this file we are writing some text into the file. In the end, we are closing the file through a file.close() method. As the texts are character therefore, we have to declare the string line character so that we can read all the texts from the file. By using getline() we are reading every character then writing it into the demo.txt file.
Example #3
Here is another C ++ code to demonstrate the working of the stream for writing into a file in programming.
Code:
#include <fstream>
#include <iostream>
using namespace std ;
int main () {
char data[100] ;
// opening a file in the write mode.
ofstreamoutfile ;
outfile.open ( " Demo.txt " ) ;
cout<< " Writing to the file " <<endl ;
cout<< " Enter your name : " ;
cin.getline ( data , 100 ) ;
// writing the input data into the file.
outfile<< data <<endl ;
cout<< " Enter your age: " ;
cin>>data ;
cin.ignore () ;
// writing the input data into the file.
outfile<< data <<endl ;
// closing the opened file.
outfile.close () ;
// opening a file in read mode.
ifstreaminfile ;
infile.open ( "Demo.txt " ) ;
cout<< " Reading from the file " <<endl ;
infile>>data ;
// writing the data
cout<< data <<endl ;
// reading the data from the file
infile>>data ;
cout<< data <<endl ;
// closing the opened file.
infile.close () ;
return 0 ;
}
Output:
Explanation: As you can see in the above code we have used fstream in our header files to include all the file and iostream classes. As we are using ostream to handle the output stream. The through outfile.open we are opening a file name ” Demo.txt”. After opening this file we are writing some text into the file. In the end, we are closing the file through outfile.close() method.
Conclusion
ifstream classes play an important role in the file handling over small as well as large complex projects which helps in modifying the files instantly without affecting the efficiency of the source code. Therefore, with the help of these stream classes, we can use the number of functions.
Recommended Articles
This is a guide to ifstream in C++. Here we discuss an introduction to ifstream in C++ with syntax and different types of file modes with examples to implement. You can also go through our other related articles to learn more –