Input and output in c + +

Input cin

cin stands for buffer. Let's take a common example

int main(){
    char input[1024];
    char c;
    cout << "input:" << endl;
    while (cin.peek() != '\n') {
        cin >> input;
        cout << input << endl;
    }   
}

When the ">" operator reads data, it will stop automatically when it encounters spaces, so it can be used to output a sentence, such as hello world, by word.

int main(){
    char input[1024];
    char c;
    c = cin.get(); //Read a character from the buffer without any parameters
    cin.get(c);	//Read a character to c in parentheses
    cin.get(input, 1024); //Read 1024 characters to input, do not read line breaks in the buffer
    cin.get(input, 1024, 'a'); //Read characters to input, discard a and a later

	//Note that the following two getline s are distinguished. The former is the member function of cin, and the latter is not
	//Read 1024 characters to input, if the length is not met, until the line break (the last line break will be read, and then discarded)
    cin.getline(input, 1024);

	//Read a row of data from cin to input
    getline(cin,input);

    cin.ignore();	//Read a character and discard it, that is, ignore a character
    cin.ignore(2);	//Read two, lose
    cin.ignore(100,'\n');	//Ignore 100 characters or until a new line is encountered

    cin.peek();	//Read a character and put it back, that is, look at a character
    cin.putback(c); //Put back one character
}
Flag and clear

Many online tutorials say that cin.sync means to clear the cache. In fact, there is a problem here. xcode's c + + compiler can't empty the cache, but g + + can. Here we use cin.ignore(1024, '\ n') instead. 1024 has a large number of tables.
cin.fail() returns whether the flag bit is valid
cin.clear() reset flag bit

int main(){
    int num = 0;
    cout << "input" << endl;
    while (true) {
        cin >> num;
        cout << "Flag bit:" << cin.fail() << endl;
        cin.clear();    //Reset flag bit
//        cin.sync(); / / clear buffer
        cin.ignore(1024,'\n');
        cout << "Flag bit:" << cin.fail() << endl;
    }
}

If you think that 1024 is not enough ignored, you can use the following method to set it. If it is file reading, use EOF, and console input uses' \ n '

int main(){
    int num = 0;
    cout << "input" << endl;
    while (true) {
        cin >> num;
        cout << "Flag bit:" << cin.fail() << endl;
        cin.clear();    //Reset flag bit
//        cin.sync(); / / clear buffer
        while (cin.peek() != '\n') {
            cin.ignore();
        }
        cout << "Flag bit:" << cin.fail() << endl;
    }
}

Output cout is slightly omitted due to less use

File operation

output to a file

ofstream ofs("./test.txt",ios::out | ios::trunc);	//out represents output to a file. It is also the default for trunc not to write. If there is no file, it will be created. If there is a file, it will delete its contents and then output.
    if (ofs.is_open()) {
        ofs << "First row of data" << endl;
        ofs << "Second row of data" << endl;
        ofs << "Third row of data" << endl;
    }else{
        cout << "File open error" << endl;
    }
    ofs.close();

It's fine too

ofstream ofs;
ofs.open("./test.txt",ios::out | ios::trunc);

Import from file

ifstream ifs;
    ifs.open("./test.txt",ios::in);
    if (ifs.is_open()) {
        char output[1024];
        char c;
        while (ifs >> output) {	//Output by line
            cout << output << endl;
        }
    }else{
        cout << "File open failed" << endl;
    }
    ifs.close();
while ( (c = ifs.get()) != EOF) {	//Character by character output
            cout << c;
        }

Tags: iOS xcode less

Posted on Sat, 02 Nov 2019 23:48:59 -0700 by Froy