The difference between fgets and gets

Source: Internet
Author: User
Tags array length strlen

In the programming found gets and fgets some differences summed up;

1, fgets than get safe, use gets compile warning


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 scary.

fgets specifies the size, and if the array size is exceeded, it is automatically truncated based on the length of the defined array. (redhat9.0 experiment is not so, do not know why, still can output)

2, with strlen detection of the input string length, the result is not the same

Gets


Fgets


As you can see, the same 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.

For Fgets, but also to say two more sentences, the following is a safe way to judge the end of the file read or error, do not use while (!feof (FP)), and for fgets the second parameter is the maximum number of characters to read the file, the general maximum length is 1024 bytes.

while (Fgets (..., stream)) {

/* ... */

}

if (Ferror (stream)) {

/* ... */

}

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.