Reading From Infile Writing to Outfile C++
In this commodity, we are going to evidence you how to read and write to a file in the C++ programming linguistic communication by using several examples. To sympathize C++ file operations like read and write, we must commencement sympathize the concept of a stream in C++.
What is a Stream?
A stream is but a menstruum of data or characters. There are two types of streams: input streams and output streams. An input stream is used to read the information from an external input device such every bit a keyboard, while an output stream is used to write data to the external output device such as a monitor. A file tin can be considered equally both an input and output source.
In C++, we employ a stream to send or to receive data to or from an external source.
We can use built-in classes to access an input/output stream, i.e., "ios".
Here is the stream class bureaucracy of the C++ programming language:
The "cin" and "cout" objects are used to read the information from the keyboard and to display the output on the monitor, respectively. In addition, "ifstream," which stands for "input file stream," is used to read a stream of data from a file, and "ofstream," which stands for "output file stream," is used to write a stream of data to a file.
The "iostram.h" file contains all the required standard input/output stream classes in the C++ programming language.
Examples
Now that yous empathise the nuts of streams, we will discuss the following examples to assistance you to amend understand file operations in C++:
- Example one: Open and Close a File
- Example 2: Write to a File
- Example 3: Read from a File
- Example 4: Read and Write to a File
- Case 5: Read and Write to a Binary File
Case i: Open and Close a File
In this example program, we will demonstrate how to open up/create a file and how to close the file in C++. As you tin meet in the below program, we have included the library required for file operations.
To open up and close a file, we need an object of ofstream. Then, to read or write to a file, we have to open the file. We take included the fstream header file at line number-1 so that we can access ofstream class.
We accept declared a myFile_Handler every bit an object of ofstream inside the principal function. We tin then apply the open up() function to create an empty file and the close() function to shut the file.
#include <fstream>
using namespace std;
int main( )
{
ofstream myFile_Handler;
// File Open
myFile_Handler.open up ( "File_1.txt" ) ;
// File Shut
myFile_Handler.close ( ) ;
return 0 ;
}
Now, nosotros will compile the plan and examine the output. As you tin run into in the output window below, the "File_1.txt" file was created after executing the plan. The size of the file is zero since we have not written any content in the file.
Case 2: Write to a File
In the previous example program, nosotros showed y'all how to open a file and how to shut the file. At present, we will show you how to write something in a file.
We can write to a file using the stream insertion operator, i.due east., "<<". In this program, we have used the file handler and insertion operator to write two lines in the file. The insertion operator ("<<") indicates that we are inserting the cord into the output file stream object.
#include <fstream>
using namespace std;
int main( )
{
ofstream myFile_Handler;
// File Open
myFile_Handler.open ( "File_1.txt" ) ;
// Write to the file
myFile_Handler << "This is a sample test File. " << endl;
myFile_Handler << "This is the second line of the file. " << endl;
// File Close
myFile_Handler.close ( ) ;
render 0 ;
}
At present, we will compile the to a higher place program and execute it. As you tin can see below, we have successfully written to the file File_1.txt.
Instance iii: Read from a File
In the previous examples, we showed y'all how to write content to a file. At present, let's read the content from the file that nosotros created in Example-2 and brandish the content on the standard output device, i.east., the monitor.
Nosotros use the getline() office to read the complete line from the file then "cout" to print the line on the monitor.
#include <iostream>
#include <fstream>
#include <cord>
using namespace std;
int principal( )
{
ifstream myFile_Handler;
string myLine;
// File Open in the Read Style
myFile_Handler.open ( "File_1.txt" ) ;
if (myFile_Handler.is_open ( ) )
{
// Proceed reading the file
while (getline(myFile_Handler, myLine) )
{
// print the line on the standard output
cout << myLine << endl;
}
// File Close
myFile_Handler.close ( ) ;
}
else
{
cout << "Unable to open the file!" ;
}
return 0 ;
}
Now, we will print the content of File_1.txt using the following command: cat File_1.txt. Once we compile and execute the program, it is clear that the output matches the content of the file. Therefore, we take successfully read the file and printed the content of the file to the monitor.
Example 4: Read and Write to a File
So far, we have showed you how to open, read, write, and close a file. In C++, we tin too read and write to a file at the same time. To both read and write to a file, we have to get an fstream object and open the file in "ios::in" and "ios::out" mode.
In this example, we first write some content to the file. Then, nosotros read the data from the file and impress it to the monitor.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int principal( )
{
fstream myFile_Handler;
string myLine;
// File Open up
myFile_Handler.open up ( "File_1.txt", ios:: in | ios:: out ) ;
// Check if the file has opened
if ( !myFile_Handler)
{
cout << "File did not open!" ;
exit ( 1 ) ;
}
// Write to the file
myFile_Handler << "1. This is some other sample test File. " << endl;
myFile_Handler << "2. This is the second line of the file. " << endl;
myFile_Handler.seekg (ios:: beg ) ;
// Read the File
if (myFile_Handler.is_open ( ) )
{
// Continue reading the file
while ( getline(myFile_Handler, myLine) )
{
// print the line on the standard output
cout << myLine << endl;
}
// File Shut
myFile_Handler.close ( ) ;
}
else
{
cout << "Unable to open the file!" ;
}
myFile_Handler.shut ( ) ;
return 0 ;
}
Now, nosotros will compile and execute the program.
Example 5: Read and Write to a Binary File
In this case, we are going to declare a grade and so write the object to a binary file. To simplify this instance, we have declared the Employee class with a public variable emp_id. Then, nosotros will read the binary file and print the output to the monitor.
#include <iostream>
#include <fstream>
using namespace std;
class Employee
{
public :
int emp_id;
} ;
int main( )
{
ofstream binOutFile_Handler;
ifstream binInFile_Handler;
Employee empObj_W, empObj_R;
// File Open up
binOutFile_Handler.open ( "Employee.dat", ios:: out | ios:: binary ) ;
// Check if the file has opened
if ( !binOutFile_Handler)
{
cout << "File did not open!" ;
exit ( 1 ) ;
}
// Initialize empObj_W
empObj_W.emp_id = 1512 ;
// Write to the file
binOutFile_Handler.write ( ( char * ) &empObj_W, sizeof (Employee) ) ;
binOutFile_Handler.close ( ) ;
if ( !binOutFile_Handler.skilful ( ) )
{
cout << "Fault occured during writing the binary file!" << endl;
exit ( two ) ;
}
// Now, let's read the employee.dat file
binInFile_Handler.open ( "Employee.dat", ios:: in | ios:: binary ) ;
// Cheque if the file has opened
if ( !binInFile_Handler)
{
cout << "File did not open up!" ;
exit ( three ) ;
}
// Read the content of the binary file
binInFile_Handler.read ( ( char * ) &empObj_R, sizeof (Employee) ) ;
binInFile_Handler.shut ( ) ;
if ( !binInFile_Handler.good ( ) )
{
cout << "Error occured during reading the binary file!" << endl;
get out ( 4 ) ;
}
// Print the output of empObj_R
cout << "Details of the Employee : " << endl;
cout << "Employee ID : " << empObj_R.emp_id << endl;
return 0 ;
}
Conclusion
Files are mainly used to store the data, and they play an important role in real-globe programming. In this commodity, we showed y'all how to employ various file operations with the C++ programming language by working through several examples. Furthermore, nosotros showed you how to read and write data into both text files and binary files.
Source: https://linuxhint.com/cplusplus_read_write/
0 Response to "Reading From Infile Writing to Outfile C++"
Postar um comentário