1, fgets than gets safe!
For security, get is less used because it does not specify the size of the input character, restricts the size of the input buffer, and if the input character is greater than the defined array length, a memory overrun occurs and the stack overflows. The consequences are very serious!
fgets specifies the size, and if the array size is exceeded, it is automatically truncated based on the length of the defined array.
2, with strlen detection of the input string length, the result is not the same
Same as input 123
Gets only one line break, this is because the program's statement printf ("%s\n", str)
Fgets has two times, and the second is itself putting a carriage return newline character into a string
So, get is only 3 of the length of the input string length, fgets is 4, more than a carriage return line break.
Specific introduction:
The Fgets function Fgets function is used to read a string from a file.
The Fgets function is called as follows: Fgets (STR,N,FP); here, the FP is the file pointer; Str is the starting address of the string, and n is an int type variable.
function is the function of the file from the FP read into the n-1 characters into the STR as the starting address of the space, if the n-1 characters are not read, read a line break or an EOF (file end flag), the end of this read operation, the read in the string is the last to include read the newline character.
So, to be exact, you can only read n-1 characters when you call the Fgets function. At the end of the read, the system will automatically add ' + ' at the end and return with STR as the function value.
Gets () deletes the new line character, and Fgets () retains the new line character.
To remove the fgets () last with the "" ", as long as the S[strlen (s) -1]= ' n ';
Fgets does not automatically remove the end-of-the-box as a get, so the program manually converts the value in the \ n position to a, representing the end of the input.
The difference between the fgets () and the Gets () function