1. The commonly used class for file operations is the fstream class.
fstreamshi rhombus inherits the class so it includes the ifstream class and ofstream class.
ifstream defines the object to read the file.
ofstream defines the object to write a file.
: How to open the file.
Open open file has two parameters: the first is the name of the file we open, and the second is the way to open it.
1) The ways to open the file are:
app: Open the file in a writable way and write it at the end of the file content, and will not overwrite the content of the previous file.
binary: Open the file in binary mode.
in: Opening a file in a read-only manner cannot change the content of the file.
out: The operation of opening a file for writing is to write data into the file.
ate: The open file is a file pointer to the end of the file, but it can be inserted at any location.
trunc: If the opened file already exists, clear the contents of the file.
Here are specific examples:
```
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
//Write the file
void write()
{
ofstream file;
("./", ios_base::app);
if (!file.is_open())
{
cout << "Failed to open the file";
}
string ss;
cin >> ss;
file << ss << endl;
();
}
//The first type of file reading operation
void read1()
{
ifstream file;
("./", ios_base::in);
if (!file.is_open())
{
cout << "Failed to open the file";
}
string s;
while (getline(file, s))
{
cout << s << endl;
}
();
}
//The second type of file reading operation
void read2()
{
ifstream file;
("./", ios_base::in);
if (!file.is_open())
{
cout << "Open failed";
}
stringstream s;
s << ();
string str = ();
cout << str << endl;
}
int main()
{
write();
read1();
cout << endl;
read2();
cout << endl;
return 0;
}
```