C + + learning notes - file operation

Posted by jtapoling on Fri, 28 Feb 2020 05:31:32 +0100

The data generated by C + + when the program is running belongs to temporary data, which will be released after the program is running. Sometimes we need to store some simple data permanently. At this time, we can use files to persist the data.

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 file: the file is stored in the computer in the binary form of text, which can not be understood by the user in general.

There are three types of file operations:

1.ofstream: write operation;

2.ifstream: read operation;

3.fstream: read / write operation.

How to open the file:

Open mode Notes
ios::in Open file, read file
ios::out Open file, write file
ios::ate Initial location: end of file
ios::app Write file, append method
ios::trunc If the file exists, delete it before creating it
ios::binary Binary mode

The file opening method can be used with the | operator.

Write operation flow of text file:

#include <iostream>
#include <string>
//1. Include header file
#include <fstream>
using namespace std;

void test01(){
//    2. Create a flow object
    ofstream ofstream1;
//    3. Open the file. The parameters are file path and opening method
    ofstream1.open("test.txt",ios::out);
//    4. writing data
    ofstream1<<"name:li"<<endl;
    ofstream1<<"age:15"<<endl;
    ofstream1<<"height:178cm"<<endl;
//    5. Close file
    ofstream1.close();
}

int main() {
    test01();
    return 0;
}

Read operation flow of text file:

#include <iostream>
#include <string>
//1. Include header file
#include <fstream>
using namespace std;

void test01(){
//    2. Create a flow object
    ifstream ifstream1;
//    3. Open the file. The parameters are file path and opening method
    ifstream1.open("test.txt",ios::in);
//    4. Judge whether the file is opened successfully
    if(!ifstream1.is_open()){
        cout<<"File open failed"<<endl;
        return;
    }
//    5. The first way to read documents
    string buffer;
    while(getline(ifstream1,buffer)){
        cout<<buffer<<endl;
    }
//    After reading the file, the stream status is eof, so set the stream status to goodbit, so that the file pointer can be moved
    ifstream1.clear(std::ios::goodbit);
//    Move the file pointer to the start, use seekg for reading files and seekp for writing files.
    ifstream1.seekg(std::ios::beg);
//    The second way to read documents
    char buffer1[1024]={0};
    while(ifstream1>>buffer1){
        cout<<buffer1<<endl;
    }
    ifstream1.clear(std::ios::goodbit);
    ifstream1.seekg(std::ios::beg);
//    The third way to read documents
    char buffer2[1024]={0};
    while(ifstream1.getline(buffer2, sizeof(buffer2))){
        cout<<buffer2<<endl;
    }
    ifstream1.clear(std::ios::goodbit);
    ifstream1.seekg(std::ios::beg);
//    The fourth way to read documents
    char c;
    while((c=ifstream1.get())!=EOF){
        cout<<c;
    }
//    6. Close file
    ifstream1.close();
}

int main() {
    test01();
    return 0;
}

Binary write operation:

Write using the member function write.

Function prototype: OFSTREAM & write (const char * buffer, int len);

Parameter: buffer is a character pointer, pointing to a section of memory space, len is the number of bytes read and write.

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

void test01(){
    ofstream ofstream1;
    ofstream1.open("test.txt",ios::out|ios::binary);
    string s="hello world";
    ofstream1.write((const char*)&s, sizeof(s));
    ofstream1.close();
}

int main() {
    test01();
    return 0;
}

Binary read operation:

Read using the member function read.

Function prototype: ifstream & read (char * buffer, int len);

Parameter: buffer is a character pointer, pointing to a section of memory space, len is the number of bytes read and write.

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

void test01(){
    ifstream ifstream1;
    ifstream1.open("test.txt",ios::in|ios::binary);
    if(!ifstream1.is_open()){
        cout<<"File open failed"<<endl;
        return;
    }
    string s;
    ifstream1.read((char *)&s, 40);
    cout<< s<<endl;
    ifstream1.close();
}

int main() {
    test01();
    return 0;
}

 

Published 15 original articles, won praise 8, visited 5894
Private letter follow

Topics: iOS ascii