Detailed explanation, c/c++ input and output buffers, and the problem of scanf carriage return

1. Output buffer

The output buffer is the default output buffer size allocated by linux/windows for us under the dos window, or under the terminal, that is, how many characters the window can hold at a time! The size of the output buffer is the same as the image, which is defined by the width and height. In other words, the output buffer is equivalent to a small screen, and the width and height of the screen are specified by the operating system/user. We can use our program runtime. Some data is entered into this buffer and displayed on the screen, as columns such as (printf/cout) do.

The output buffer is when we run a c/c++ program based on a dos window, the operating system will call a dos window for us by default and load the program file into the dos system, which is run by dos!

The output buffer is the default, but we can adjust it manually!

Output buffer adjustment method

Move the mouse cursor focus to the title bar of the dos window, then right-click


select properties


Choose a layout


Adjust the screen buffer size here


Here mine is 80*300, which means that my screen buffer can store the maximum size: 24000 pixels!

Once it exceeds this range, it will delete and incrementally replace the original buffer data!

as

1 1 1

2 2 2

3 3 3

At this point, the buffer is full, and if you write data to it, the first line will be replaced.

2 2 2 

3 3 3

4 4 4


2. Input buffer

It is used to save the temporarily input data when your program is running, that is, the data we usually input on the dos window will be placed in the input buffer, but it will also be displayed in the output buffer when input!

For example, you are inputting data on the dos window, but your dos window output buffer size is 5, then when you input more than 5 pixels, the original output buffer data will be replaced, but the input will not be replaced Buffer data!

Conversely, if your input buffer fills up, it will also be replaced incrementally!

When you enter the following data in a screen buffer that can only hold 5 pixels

12345 (normal at this time)

When you enter the 6th data, it will incrementally replace

23456

3. The problem of scnaf carriage return

I don't know that you usually have such problems when writing c/c++ programs

Columns such as:

int a;
	scanf("%d%[\n]", &a);
	printf("%d", a);
	int b;
	scanf("%d%[\n]", &b);

When you use scanf to get data, when you press Enter when you have finished entering the value of the a variable, the scanf that receives b skips directly!

The reason for this problem is that scanf does not receive carriage returns, but ends with carriage returns. When you enter data on the dos window, it will be input into the input buffer, and scanf will read the input buffer. For these data, scanf will use the carriage return as the EOF (end) flag, but it will not read the carriage return! So the carriage return stays in the input buffer at this time, and it will be read out when we execute the next scanf! And it happens that scanf ends with a newline, so the phenomenon of direct skipping occurs!

Solution:

Note that this only works when typing characters!

No such problem occurs when you use %d or other format placeholders of type integer!

1. Flush the input buffer

fflush(stdin);

stdin: input buffer, generally refers to the data input by the keyboard, the data can only be input by the keyboard, of course, you can also copy, but that is also the data typed by the keyboard!

2. Use getchar to read the buffer data

getchar();

3. Use regular expressions

Enter the following code below scanf scanf will automatically read a space from stdin

scanf("%[^ ]"); does not end with a carriage return, it ends with a space, it is only valid when reading a string!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325784625&siteId=291194637