Files can keep data in a storage unit for a long time. While the data is in the register, it can be subjected to a variety of operations. The programs store outputs, and other data of the program to a file using file handling in C.
Learn more about software development and jumpstart your career with our Caltech Coding Bootcamp.
File Handling
Sometimes, to process a code to the next phase, we need to read some files while coding. And we can read any required file from any location with stream classes. This process is also known as file handling.
Stream Classes in C++
A stream is a representation of a computer that performs input/output operations. It can be viewed as either a destination or a source of indefinitely long characters which can be decided by the way they are used.
fstream in C++ comes with a library that includes methods for dealing with files.
- ofstream- This class describes an output stream. It is used to create files and to write data to files.
- ifstream- This class describes an input stream. It's a program that reads data from files and displays it.
- fstream- This class describes a file stream in general. It has ofstream and ifstream capabilities. This means it can create files, write to files, and read data from files.
To use the fstream library's above classes, you must include it as a header file in your program. By default, the #include preprocessor directive will be used. The iostream header file must also be used.
Syntax of ifstream in C++
When you're coding for ifstream c++, you'll occasionally need to read a file in order to move on to the next phase, and you'll need something in your code to assist you in reading the required file from any location. This is also known as file handling, and it requires stream classes. The fstream, ofstream, and ifstream classes are used to accomplish this. Ifstream c++ is a file input stream that allows us to read any information contained in the file. We need to include the iostream> and fstream> header files in our code to use these stream classes.
Syntax:
ifstreamobject_name( “file_name ” ) ;
We've seen what ifstream is and how we can use it in our code to perform various operations, such as reading a file, writing to a file, or accessing a file. We'll also see how ifstream works by looking at some ifstream c++ code examples.
Different Types of File Modes
Let's look at the different types of file modes that can be used in our source code.
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 in ifstream c++ 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 of ifstream
Example 1:
#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:
In the above program, we created a file with the name file in the preceding ifstream c++ code, and then we created an ofstream object so that we could write to the file after opening it with the file. open syntax. And after writing the information into the file, we finally closed it.
Example 2:
#include <iostream>
#include <string>
#include <fstream>
using namespace std ;
int main() {
// Writing 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:
Example 3:
#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:
Conclusion
The ifstream classes play an important role in file handling in both small and large complex projects, allowing for instant file modification without compromising the source code's efficiency. As a result, we can use a variety of functions with the help of these stream classes.
If you want to learn more about the concept of ifstream c++, you must enroll in the Post Graduate Program In Full Stack Web Development offered by Simplilearn. Pursuing ifstream c++ courses and other courses in this program will ensure a promising career and help you find rewarding job opportunities.
You can also check out the SkillUp platform, an initiative by Simplilearn, where you’ll find free online skill-up courses in domains like data science, business analytics, software development, AI, and machine learning.
Explore all free courses, free career guides, interview tips and techniques, and much more with Simplilearn.