CPP standard library fstream is used to create a file, to read the data from a file and to write the data into a file in CPP programming. There are three data types defined in fstream library. These are:
fstream:
Used to create a file, to read the data from a file and to write the data into a CPP file.
ifstream:
Used to read the data from a CPP file.
ofstream:
Used to create a file and to write the data into a CPP file.
Example:
#include <iostream> #include <fstream> using namespace std; int main () { char arr[100]; ofstream str; str.open("myfile.txt"); cout <<"Saving your informations." << endl; cout << "Dress Required: "; cin.getline(arr, 100); str << arr << endl; cout << "Dress Color Required: "; cin >> arr; cin.ignore(); str << arr << endl; str.close(); cout << "\n"; ifstream str2; string line; str2.open("myfile.txt"); cout << "Review the Saved informations." << endl; while (getline (str2,line)) { cout << line << endl; } str.close(); return 0; } |
Output
Saving your informations Dress Required: T Shirt Dress Color Required: White Review the Saved informations. T Shirt White |