C buffer learning 2-getchar GETC getch getche gets Function

Source: Internet
Author: User

Buffer-related operations are common operations that allow input and output of our characters. In fact, after accessing the operating system and compilation principles, it seems that the computer is actually processing strings. Today we will introduce several functions of the get family.

Many initially use C language for string operations, and realize that the buffer issue is generally fromProgramStart:

Many beginners may ask why the second Input Program is over without pressing the Enter key, which is the reason for the buffer. The getchar function is the first stdio family to be introduced today.

(1)Int getchar (void)

Each time a function reads a character from the stdin buffer, the carriage return is returned, and the carriage return is saved to the buffer. Therefore, getchar can be used to eat the carriage return.

The function returns the ASC code value of the first character. If it fails, it returns-1.

It is defined as a macro definition, that is# Define getchar () GETC (stdin)

Getchar does not support pointer reference because it is a macro-defined function.

Now explain the reason for skipping the second input. If we enter a for the first time and press enter, then getchar actually puts the character and press enter into the buffer and returns the value of the first character, then the second getchar function continues to remove characters from the buffer and returns the carriage return. You do not need to enter the characters again. For more information about this mechanism, see

Http://www.cnblogs.COM/octobershiner/archive/2011/12/06/2278492 .html

Therefore, many people use getchar to eat the carriage return, which is similar to clearing the buffer zone. You can also use the fflush function to clear the buffer, but this function is not a function in the C standard library, and sometimes it is invalid.

The definition of getchar is actually a special case of the GETC function. The following describes the GETC function.

(2)Int GETC (File * Stream)

Macro definition is also used, so function pointer calls are not supported.

 # Define GETC (_ stream) (-- (_ stream)-> _ CNT> = 0? 0xff & * (_ stream)-> _ PTR ++: _ filbuf (_ stream ))

GETC reads a character from the specified stream. The getchar is actually GETC (stdin), stdin is the standard input stream, and C is in stdio. h defines three streams, which can also be understood as a buffer zone.

# Define stdin (& _ iob [0]) // standard input, generally pointing to the keyboard
# Define stdout (& _ iob [1]) // standard output
# Define stderr (& _ iob [2]) // error stream

Basic implementation process

Here, I will add two more functions, which are simply taken over, because getch and getche are not c-standard library functions.

(3)Int getch (void)

Reading a single character from the command line is not displayed in the command line. Many people use it to simulate the effect of pressing any key to continue"

(4)Int getche (void)

Read a character from the command line and display it in the command line. Like getch, it is not a function in the Standard C function library. It contains conio in windows. in H, curses is the opposite in Linux. but the GCC compiler cannot be used in UNIX and Ubuntu Linux simulated by cygwin.

At the end of this article, we will introduce the gets function, which reads strings from the stream until it encounters a line break or EOF or an error.

(5)Char * gets (char * buffer)

Reads a string from the stdin stream until it receives a linefeed or EOF, and stores the read results in the character array pointed to by the buffer pointer. The linefeed is not used as the content of the read string. The read linefeed is converted to a null value and ends the string.

 

 

In fact, the gets function still has a lot of security issues that need to be paid attention to. For example, it has to wait until the EOF or line break ends, so when the program cannot predict the possible length of the input string, it is very dangerous.ArticleWhen we mentioned the buffer overflow attack, this is a vulnerability. We cannot compare it with the buffer size with the attacker. in a timely manner, we define a large buffer zone, attackers can enter a longer string.

Among them, VS 2005 provides a new function gets_s with security, but this is clearly not defined in the standard library.

========== F split line ==========================

Stdio. H also contains file operation functions that you want to perform with gets and GETC, fgets and fgetc.

Char * fgets (char * s, int N, file * stream); // n indicates the length of data read at a time, which is read from the stream and saved to the S string, ends when a line break or end character is encountered.

Int fgetc (File * stream );


the series of put functions will be shared later ....

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.