File handling and stream


This requires another standard C ++ library called fstream, which describes three types of data -

ofstream This type of data represents the output of the output and is used to create files and write details to files.

ifstream This type of data represents the input file stream and reads the information in files.

fstream This type of data represents a general file distribution and can both stream and navigate, which means it can create files, record information, and read data to files.

To perform file processing in C ++, the header files and must be included in your C ++ source file.

Example:

#include <iostream> #include <fstream> using namespace std; int main() { // Variable Declaring ofstream class object ofstream know; string str1; know.open("test.txt"); while (know) { getline(cin, str1); if (str1 == "-1") break; know << str1 << endl; } know.close(); ifstream file; file.open("test.txt"); while (file) { getline(file, str1); cout << str1 << endl; } file.close(); return 0; }

OUTPUT:

string type

In C ++, files are mainly managed using three streaming streams found in the fstream header file.
ofstream: Stream class to write to files
ifstream: Stream class to read files
fstream: Stream class for reading and writing from/to files.