Transfer from http://blog.csdn.net/todd911/article/details/8952565
Input/output function family
Family names available for all flows only for stdin and stdout
GetChar character input fgetc,getc GetChar
Putchar character output FPUTC,PUTC Putchar
Gets text line input fgets gets
Puts text line output fputs puts
scanf format Input fscanf scanf
printf formatted output fprintf printf
Note the point:
Fgetc and FPUTC are real functions, but Getc,putc,getchar and Putchar are all macros defined by the # define directive,
Therefore, parameters with side effects cannot be used when calling Getc,putc,getchar and Putchar.
The usage of fgets is as follows:
Char *fgets (char *string,int n,file *stream);
Fgets () is used to read the characters from the file referred to in the parameter stream to the memory space referred to in the argument string until a newline character is present, the end of the file is read, or the n-1 character is read, and then NULL is terminated as a string. If a newline character or an EOF (end-of-file) is read when the n-1 characters are not read, the read-in string ends with the read-only newline character, and a null. If the string cannot hold an entire row, the next call to Fgets will begin reading from the next character of the stream without data loss.
Fputs writes a string to the specified file (does not automatically write the string end tag ' \ ")
Gets reads a string from the stdin stream until it is accepted to a newline or EOF, and the result of the read is stored in the character array pointed to by the Str pointer. The newline character is not read as the contents of the string, and the read newline character is converted to a null value and thus ends the string.
The Get function is unsafe, does not limit the size of the input buffer, it is prone to overflow, so try not to use the gets.
About gets endangerment:
I write the following procedures:
1 #include <stdio.h>2 int main (void) 3 {4 char name[10];5 gets (name); 6 puts (name); 7}
This program is the simplest, is a string output input problem, but the use of Linux under the GCC compiler is always unable to pass, the question is:
"The ' gets ' function is dangerous and shout not being used."
Let me be very tangled, why this function is not allowed to use, how is it dangerous?
Then I looked at the Linux C library and I got it. The book added a note to this function: "Because get () cannot know the size of the string, you must encounter a newline character or end of the file to finish the input, so it is easy to create a cache overflow security issue. It is recommended to use Fgets () instead. ”
Originally, GCC has canceled the right to use the Get () function directly for security reasons. Below is a small program that net boy compiles with VC under Windows, to illustrate what harm gets ().
The procedure is as follows:
1 #include <stdio.h> 2 int main (void) 3 {4 char A; 5 char name[3]; 6 char b; 7 scanf ("%c", &a); 8 printf ("a:%c\n", a); The value of the first output A is 9 getchar (); ( name); scanf ("%c", &b) ; printf ("name:%s;\na:%c;\nb:%c;\n", name,a,b); Output all values, note A13}
The input string is ABC, which exactly achieves the desired effect. Let's add the length of the string and then test it to see what the effect is:
Because we changed the string to "this is a text"
Then the value of a was somehow replaced. The compiler does not work properly immediately.
The damage caused by improper use of the Get () function is very large, as we have just seen, the value of a is replaced by the value of the string name overflow. I think this is not the result you want! For safety, use fgets () instead of gets ()!
Fgetc,getc,getchar,fputc,putc,putchar ....