c + + file operation

catalogue

File operation

text file

Binary file

File operation


The data generated when the program runs belongs to temporary data. Once the program runs, it will be released
Data can be persisted through files
File operations in c + + need to include the header < fstream >

There are two types of files:
1) text file - the file is stored in the computer in the form of ASCII code of text
2) binary files - files are stored in the computer in binary form of text, and users generally can't read them directly

Three categories of operation documents:
1) ofstream: write operation
2) ifstream: read operation
3) fstream: read / write operation

text file


The steps for writing files are as follows:
1) include header file #include < fsstream >
2) create a stream object oftream ofs;
3) open the file ofs Open ("file path", opening method)
4) write data ofs < < "written data";
5) close the file ofs close();
File opening method:

Opening method} explanation
ios::in: opens a file for reading
ios::out: open file for writing
ios::ate initial location: end of file
ios::app: write files in append mode
ios::trunc: if the file exists, delete it first and then create it
ios::binary: binary mode

Note: the file opening method can be used in conjunction with the | operator

Example (read):

        ofstream ofs;
        ofs.open("test.txt",ios::out);
        ofs << "Name: Li Si" << endl;
        ofs << "Age: 18" << endl;
        ofs << "Gender: Male" << endl;
        ofs.close();


    
Summary:
1) the file operation must include the header file fstream
2) ofstream or fsstream class can be used to read files
3) when opening a file, you need to specify the path of the operation file and the opening method
4) use < < to write data to the file
5) close the file after the operation
    
Example (write):

        //char buf[128] = {0};
        ifstream ifs;
        ifs.open("test.txt",ios::in);
        if (ifs.is_open() == false) {
            cout << "fail to open file" << endl;
            return;
        }

        //First kind
        //while (ifs >> buf) {
        //    cout << buf << endl;
        //}

        //Second
        //while (ifs.getline(buf, sizeof(buf))) {
        //    cout << buf << endl;
        //}

        //Third
        string buf;
        while (getline(ifs, buf)) {
            cout << buf << endl;
        }

        //Fourth
        //char c;
        //while( (c = ifs.get()) != EOF ) {
        //    cout << c;
        //}

        ifs.close();

Summary:
1) i fstream or fsstream class can be used to read files
2) using is_ The open function can judge whether the file is opened successfully
3) close close the file

Binary file


Read and write files in binary mode
The opening method should be specified as ios::binary

Write binary
Binary file writing mainly uses the stream object to call the member function write
Function prototype: ostream & write (const char * buffer, int len);
Parameter explanation: the character pointer buffer points to a storage space in memory. len is the number of bytes read and written

Example:

        ofstream ofs("Preson.txt", ios::out | ios::binary);

        //ofs.open("preson.txt",ios::out | ios::binary);

        Preson p = {"Zhang San",18};
        ofs.write( (const char*)&p,sizeof(p) );

        ofs.close();

Summary: the file output stream object can write data in binary mode through the write function

Read binary
Binary file reading mainly uses the stream object to call the member function read
Function prototype: istream & read (char * buffer, int len);
Parameter explanation: the character pointer buffer points to a storage space in memory. len is the number of bytes read and written

Example:

        ifstream ifs("Preson.txt", ios::in | ios::binary);

        if (ifs.is_open() == false) {
            cout << "fail to open file" << endl;
        }

        Preson P2;

        ifs.read((char*)&P2, sizeof(P2));

        cout << P2.m_name << endl;
        cout << P2.m_age << endl;

        ifs.close();

Summary: the file input stream object can read data in binary mode through the read function

Keywords: C++

Added by eugeniu on Thu, 23 Dec 2021 04:39:50 +0200