File operations

The data generated by the program is temporary data, and will be released once the program is finished running. Data can be persisted through files. In c++, the operation of files is protected by header file <fstream> file type: text file: files are stored in the computer as ASCII text code, which can also be TXT open binary: Files are stored as binary text. There are three types of files that can be manipulated on a computer: ofstream: write operations ifstream: read files fstream: read and write filesCopy the code

Operation process

#include<fstream> 3. Open the file ofs.open(" file path ", open method); Write data ofS <<" Write data "; 5. Close ofs.close(); File opening mode: ios::in: opens a file for reading ios::out: opens a file for writing ios:: ATE: Initial location: end of the file ios::app: Appends a file ios:: Trunc: If the file exists first remove in creating ios: : binary: binary mode Main file open mode can be used Using | operation symbols ios: : binary | ios: : out instances: / / text file write void test01 () {/ / 1. Ifstream is a read file ofstream ofs; Ofs. open("test. TXT ",ios::out); Ofs << "name: zhang SAN" << endl; Ofs << "gender: male" << endl; Ofs << "age: 18" << endl; Ofs.close (); } #include<fstream> 2 Create a stream object ifstream ifs; 3. Open the file and check whether the file is open ifs.open(" file path ", open method); 4. Read data: four ways to read ifs<<" written data "; 5. Close the file ifs.close(); // Read file void test02() {//1. Create a stream object ifstream ifs; Ifs. open("test. TXT ",ios::in); if (! Ifs.is_open ()) {cout << "failed to open file" << endl; return; Char buf[1024] = {0}; char buf[1024] = {0}; //while (ifs >> buf) { // cout << buf << endl; Char buf[1024] = {0}; char buf[1024] = {0}; //while (ifs.getline(buf, sizeof(buf))) { // cout << buf << endl; //string buf; //while (getline(ifs,buf)) { // cout << buf << endl; // char c is not recommended; // char c is not recommended; while ((c = ifs.get()) ! = EOF) {//EOF end of file cout << c; } //5. Close ifs.close(); }Copy the code

Binary file

Ostream write(const char * buffer, int len); Class Person {public: char m_Name[64]; class Person {public: char m_Name[64]; // the name of the file should be used as char instead of string int m_Age; }; Void test03() {//1. Contains header files //2. Create stream read and write ofstream ofs; / / 3. Write files, open the way ofs. Open (" person. TXT ", the ios: : out | ios: : binary); Person p = {" c ",18}; ofs.write((const char *)&p, sizeof(Person)); Ofs.close (); }Copy the code

Binary read file

istream& read(char *buffer,int len); Void test04() {//1. Contains header files //2. Create a stream object ifstream ifs; / / 3. Open the file read way ifs. Open (" person. TXT ", the ios: : | in ios: : binary); if (! Ifs.is_open ()) {cout << "Failed to open!" <<endl; } //4. ifs.read((char*)&p, sizeof(Person)); Cout < < "name:" < < p. _Name < < < < "age:" p. _Age < < endl; //5. Close the file ifs.close(); }Copy the code