[Discussion] standard stream Conjecture

Source: Internet
Author: User

Article Source Address: http://www.programfan.com/club/showbbs.asp? Id = 93427
Author: CCPP
Posting time: 10:40:00
Reprinted on:
Reprinted reason: When I was working on a program today, I encountered a problem with the input stream. For a moment, I had to find information on the Internet and accidentally found this article. After reading this article, I had benefited a lot, so I collected it.

Body:
Cfaq on the Internet always says to be cautious when using fflush (stdin), or should not be used. I personally think that as long as I understand the principles of the stdin stream, there should be no problems in use. During the process of organizing these materials, I found that many of the theoretical problems were not as hard to understand as I imagined, but the "advanced" people hidden a lot of theoretical details in the Process of expressing them.
Let's talk about two major streams: The standard library streams are divided into two types: Text streams (or plain streams) and binary streams.
The text stream regards the file as a sequence of rows. Each line contains 0 or more characters, and the last line of a row contains the line break symbol '/N '. The text stream is suitable for general output and input, including human-related input and output.
Binary stream is used to directly store memory data into files in the internal form. The binary stream operation ensures that the data is read back in the same way after the file is written. The information format and content remain unchanged. Binary streams are mainly used to store and reload internal data in a program. During the operation, information is not converted, and there is a speed advantage when saving or loading a large number of data, however, this storage format is not suitable for reading.

Go to the topic,
Int main ()
{
Char C1, C2;
Scanf ("% C", & C1 );
Scanf ("% C", & C2 );
Printf ("C1 is % C, C2 is % C", C2/1, C2 );
Return 0;
}
Run the program, enter AB, and press Enter. The output result is C1 is A, C2 is B.
If you enter a character a and press enter (you must press enter to complete the input), then the output ends directly as above.
The result is C1 is A, C2 is, and the variable C2 outputs a line break (because the carriage return after character a is assigned to C2 ).
To solve the above problem, you can add the clear function fflush () before entering the function to modify the above program:
Int main ()
{
Char C1, C2;
Scanf ("% C", & C1 );
Fflush (stdin);/* can also be written as getchar (); to achieve the same effect */
Scanf ("% C", & C2 );
Printf ("C1 is % C, C2 is % C", C1, C2 );
Return 0;
}
Run the program. If you enter a character and press enter, the program waits for the next character to be entered. Fflush (stdin); is used to clear character a and press Enter. If you use getchar (); you can also read character a and press enter to achieve the same effect!

The following describes the standard stream:
When the program starts, three text streams are pre-defined (three file pointers are created and the specified values are specified, but they do not need to be explicitly opened): standard input stream (the pointer is named stdin) stdout and stderr ). Stdin is usually connected to the standard input of the operating system, stdout is connected to the standard output of the operating system, and stderr is usually directly connected to the monitor. This means that stderr cannot be reoriented (used to output diagnostic information ). When the stream is opened, the standard error stream is not fully buffered. When and only when the stream is not connected to the interactive device, the standard input and standard output streams are fully buffered (a common method for connecting to the input device, for example, when you need a keyboard to input data, when the output buffer is full, it will be refreshed and displayed on the screen. This is not a full buffer ).
To use an external file as an input/output object, you can use the standard input/output to redirect the object to a specified file. This can solve some problems. However, this practice has great limitations, because the formation of such orientation cannot be changed during program execution. To facilitate the use of various files in the program as needed, you must use the file operation functions (such as fopen) of the standard library ), you can use specific input and output streams for related files.

Next we will only study stdin:
1. When you press the keyboard, we get the scan Code (the scan code has two bytes, the low byte is ASC ⅱ code, and the high byte is the key bit code), instead of ASC ⅱ code. The scan code of the key enters the buffer zone of the keyboard. If the program has a standard input function, then after entering enter, the key data (SCAN code to convert ASC ⅱ code to form a text stream) is sent to the input buffer (stdin) with a carriage return ), after receiving data, the standard input function will leave unprocessed data in the input buffer (stdin). The consequence is that the subsequent input function will continue to read the residual data in the input buffer.
If the data content left in the buffer (stdin) is useless, the carriage return in the preceding example needs to be processed. Getchar () can be used for loop processing, or fflush (stdin) can be used to clear the buffer.
Conclusion: During this process, you must note that when the program executes the standard input function, other key scanning codes before the Enter key scan code in the buffer zone of the keyboard, is converted into a text stream to enter the standard input stream.
2. The non-standard input functions include getche (), getch (), and bioskey (0 ). when using such functions, you do not need to press the "enter" key, that is, they directly read the scan code of the keyboard buffer, which is special.
If no scan code is required in the keyboard buffer before getch () is used, clear it! The following are the functions that implement this function.
Void clear_key_buffer (void) // clears the keyboard buffer function
{
Int offset;
Offset = PEEK (0x40, 0x1a );
Pokeb (0x40, 0x1c, offset );
}
3. Test the function. The annotated function can be used to study the above theory.
After the program runs, enter 1234 and press enter 5.
The result is: C1 = 1, C2 = 2, C3 = 5, C4 = 3.

# Include <stdio. h>
# Include <conio. h>
# Include <dos. h>
Void delay (INT clicks)/* latency function */
{
Unsigned int far * Clock = (unsigned int far *) 0x0000046cl;
Unsigned int now;
Now = * clock;
While (ABS (* Clock-now) <clicks ){}
}
Void clear_key_buffer (void)
{
Int offset;
Offset = PEEK (0x40, 0x1a );
Pokeb (0x40, 0x1c, offset );
}
Int main (void)
{
Char C1, C2, C3, C4;
Int I;

For (I = 0; I <5; I ++)
Delay (20 );
/* Clear_key_buffer ();*/
C1 = getche ();
C2 = getchar ();/* only receives the input buffer */
/* Fflush (stdin );*/
C3 = getche ();/* only receives the Keyboard Buffer */
C4 = getchar ();

Printf ("/NC1 = % C, C2 = % C, C3 = % C, C4 = % C", C1, C2, C3, C4 );
Getch ();
Return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.