c++ input and output

Cin and cout are a very common syntax for writing C++, but the input and output of C++ are still worthy of in-depth study. Slowly add~The connection between the program terminal and the input stream stores the output part of the stream in the
insert image description here
program , likewise the connection of the terminal to the output stream outputs the data in the program to the output part of the stream.
Buffers are usually used to efficiently process input and output:
For hard disks: hard disks usually transfer data in blocks (512 bytes or more), programs usually process data in 1 byte, and use buffers to balance the rates at both ends.
For the keyboard: Allows the user to go back and correct the input before transferring it to the program.

Two usages of getline:
usage 1: cin.getline (character array (or character pointer), number of characters n, termination flag character)

istream& get(char*intchar);
istream& get(char*int,);
istream& getline(char*intchar);
istream& getline(char*int);

The first parameter is the location where the output of the input stream is stored in the program, the second parameter is how many characters, and the third parameter is the delimiter (so far)

Another usage: To #include, split the input stream ss with char and read it into letter

getline(stringstream ss,string letter,char);

This feature can be used in brushing questions, first use stringstream to convert the string to be split into an input stream and then use getline to split: reverse the words in the string , simplify the path

It is also possible to treat the file as an input stream, such as a code to extract the QP and bit rate of the encoded information:
insert image description here

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

int main()
{
    
    
	ofstream qpoutfile("qpout.txt", ios::trunc);
	ofstream bitoutfile("bitout.txt", ios::trunc);

	ifstream qpinfile("data.txt");
	ifstream bitinfile("data.txt");
	if (!qpinfile.is_open()&&!bitinfile.is_open())
	{
    
    
		cout << "can not open this file" << endl;
		return 0;
	}
	string letter;
	while(getline(qpinfile, letter, ')'))
	{
    
    
	string qp =letter.substr(letter.size()-3);
	qpoutfile << qp << endl;
	}
	while (getline(bitinfile, letter, 'b'))
	{
    
    
	letter.pop_back();//最后的空格去掉
	int last_index = letter.find_last_not_of("0123456789");//从末尾向前找到非数字的索引
	string bit = letter.substr(last_index + 1);
	bitoutfile << bit << endl;
	}
	qpinfile.close();
	bitinfile.close();
	qpoutfile.close();
	bitoutfile.close();
	return 0;
}

Points to note:
00, putting getline in while will continuously read each line in txt, and when the end of the file is read, it will not be able to enter while
01, so when using getline to process the file input stream, it will finally access the end of the file, and then getline will access There are no new things, so open two file streams to process QP and bit
02 respectively, getline will only recognize the newline character and the set separator, and will save the space, so it may be necessary to perform the space removal operation
03, letter. find_last_not_of("0123456789");//Find the non-number index
04 from the end forward. If substr() has only one parameter, it is the starting point of the parameter, and then it goes to the end
05. It should be better to use regular expressions for matching, but not Too good at regular expressions, so I used some str library functions according to the particularity of this example

Extract result:
insert image description here

insert image description here

The difference between get and getline is that get keeps the delimiter in the input queue, while getline does not

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

int main()
{
    
    
	char s[30];
	cin.getline(s,10,'#');
	cout << s<<endl;
	char ch;
	cin.get(ch);
	cout << ch << endl;
	return 0;
}

insert image description here
Enter ABCD#E123123132 and press Enter. At this time, press Enter to refresh the buffer first, and then inject ABCD#E123123132 into the buffer. At this time, the program runs to intercept the cin.getline(s,10,'#');
first ten characters of ABCD#E123123132 and divide them with #, and save ABCD In s, the following cin.get(ch);will read the following E in the read buffer into ch.

Guess you like

Origin blog.csdn.net/qq_42567607/article/details/125264018