Distinguish Getch, GetChe, Fgetc, Getc, GetChar, Fgets, Gets

xiaoxiao2021-03-06  90

First, the two functions are not a function in the C standard library, and INT getch // reads a character from the standard input. When you type with a keyboard, the screen does not display the characters you entered. That is, don't come back. INT getChe (Void) // reads a character from the standard input, the keyboard is entered, the screen displays the input character. Brought back. These two functions are included in the header file Conio.h, and it is necessary to remember that conoo.h is not the header file in the C Standard Library. The C compiler of MICORSOFT and BORLAND provides Conio.H to create a console text user interface. VS, VC, etc. can generally be installed under the Windows system, you can include conoe.h header files. But generally in UNIX, Linux system, / usr / include / there is no such file. Getch and getChe are waiting for the user from entering the keyboard, and if the user presses a button, do not need to press Enter, and the program is automatically executed. In Linux, the terminal input is "one pot" in the default, that is, the whole line input is processed together. Typically, this is a convenient way to people, but it also means that you must press Enter to indicate the input line when reading data. In the game, there are many "bosses", and its implementation is to take advantage of these two functions. Second, in addition to getch and getche, others are head files in the C Standard library, included in the header file stdio.h. INT FGETC (File * stream); // reads a character from the stream stream. You can enter Stdin as its argument, which reads a character from the standard input. INT getc (file * stream); // and fgetc is equivalent to the FGETC through macro. INT getchar (void); // read a character from the standard input stdin, the program waits you when you entered, you can enter multiple characters and press the computer to continue executing. / / But getchar only reads a character description: getc, getchar is implemented with the FGETC implementation through the macro definition. For example, GetChar is implemented, # define getchar () Fgetc (stdin). CHAR * FGETS (Char * STR, INT NUM, FILE * STREAM); // Read Up to NUM characters from the stream stream to the character array STR, when you encounter a newline, or when NUM-1 characters are read stop. // Automatically add the '\ 0' empty character end char * gets (char * STR); // read a string from the standard input stdin, and will be terminated at the end. / / Different from FGETS, he did not specify NUM, so it is necessary to pay attention to the size of the character array STR. Note: There is no macro definition between FGETs and GETs, each other has its own implementation. The realization of the worm is the "credit" of the function gets. The task of the Gets function is to read a string from the stream. Its caller will tell it where to put the read string. However, the Gets () function does not check the buffer size, if the caller provides a pointer to the stack, and the number of characters read in the GET () function exceeds the size of the buffer, GET () will happily Continue to write more characters into the stack, which covers the original content in the stack.

Such as: main () {char line [512]; // Assign 512 characters on the stack of the program ... gets (line); // Worm's entrance, you can write a malicious code through more data Into the stack} Never use getch and getche because they are not a function in the C standard library. The programs whose program written, different compilers are not guaranteed to contain cono.h. It is recommended to completely replace the gets function with the FGETS function. In addition, most of these GET functions have corresponding PUT versions. INT FPUTC (INT Character, File * Stream); INT PUTC (INT Character, File * Stream); // Implement INT PUT PUTCHAR (INT Character) through macro definition and fputc; // DETATIVE: #DEfine Putchar (c) FPUTC (C, STDOUT) INT FPUTS (Const Char * STR, File * Stream); int Puts (const char * str); Description: There is no macro-defined implementation relationship between the two. PUTS (Const Char * STR) approximates FPUTS (COSNT Char * STR, STDOUT), different points are the former also output a '\ n' final, about EOF EOF is the symbol constant defined in the stdio.h file, value To -1. For example, the FPUTC function returns a value: if the output is successful, the return value is the output character; if the output fails, it returns an EOF. FGETC function read characters When you encounter a file end, the function returns a file end tag EOF. If you want to read characters from a disk file and display on the screen, you can: ch = fgetc (fp); while (ch! = EOF) {PUTCHAR (CH); CH = fgetc (fp);} Note, EOF is not Output characters, so you cannot display it on the screen. Since the ASCII code is not possible - 1, EOF is defined as -1 is appropriate. When the read character value is equal to -1 (ie, EOF), it is not a normal character that is read in, but the file end value. But the above is only suitable for reading the text file. Now ANSI C has allowed to process binary files with a buffer file system, and the value of binary data read in a one byte may be -1, and this is exactly the value of EOF. This will have a need to read useful data, but it is handled as "file end". FeOf (fp) is used to test whether the current status pointed to by the FP is "file end". If you want to read a binary data in order, you can: while (! Feof (fp)) {c = fgetc (fp); ...} ================== ==========================================================================================================================================================

Three characters Enter GetChar, Getch, GetChe (C language)

Getch, getchar used two times, but met GetChe, first time, at this time, I will know in a small code part. Code 1:

-------------------------------------------------- ---------------------- Getch ()

#include #include

Int main () {char ch; printf ("Enter a character:"); ch = getch (); Printf ("\ n Your character is: '% c' \ n", ch); return 0;}

-------------------------------------------------- ------------------summary:

When I entered 6

The character is displayed in the next line, and my input is not input, and there is no carriage return.

Code 2:

-------------------------------------------------- --------------- GetChar ();

#include #include

Int main () {char ch; printf ("Enter a character:"); ch = getchar (); printf ("\ n Your character is: '% C' \ n", CH); return 0;}

-------------------------------------------------- -------------------

Summary: I entered 6 after entering the character, I entered in, the input displayed at this time, and the middle space is

Code 3:

-------------------------------------------------- -------------------------------------------------- ---- GetChe ();

#include #include

INT main () {char ch; printf ("Enter a character:"); ch = getche (); printf ("\ n Your character is: '% c' \ n", ch); return 0;}

-------------------------------------------------- --------------------------------------------------

Summary: I just entered a 6 and didn't enter the car, the input character display, and automatically transferred the next line to continue.

-------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -----------------------------

After the above code is running, the comparison after running, three character inputs, more or less subtle differences, through running screenshots, has been seen. I want readers to try themselves, in order to experience, in fact, test code is very simple, and very modular, try it!

转载请注明原文地址:https://www.9cbs.com/read-96515.html

New Post(0)