Difference between gets and fgets Functions

Source: Internet
Author: User
1. Gets and fgets

Gets function prototype: char * gets (char *Buffer); // Read characters to the array: gets (STR); STR is the array name.

Gets function: Enter characters on the keyboard until the linefeed or EOF is received, and store the read results in the character array pointed to by the buffer pointer.

The linefeed to be read is converted to a null value and used as the last character of the character array to end the string.

Note: The gets function does not specify the size of input characters, so it will be read infinitely. Once the input characters are greater than the length of the array, the memory will be out of bounds,

This causes program crash or other data errors.

Fgets function prototype: char * fgets (char * s, int N, file * stream); // we can use fgets (STR, sizeof (STR), stdin) as usual );

STR is the first address of the array, sizeof (STR) is the size of the array, and stdin indicates that we input data from the keyboard.

Fgets function: read characters from the file pointer stream, save to the space starting with S, know to read the N-1 characters, or read a line.

Note: when calling the fgets function, you can only read a maximum of N-1 characters. After reading, the system automatically adds '\ 0' at the end and returns the result using STR as the function value.

2. Details

1. As mentioned above, the gets function can be read without limit. However, some documents indicate that it can only read a maximum of 1024, so I wrote the following code to test it.

(If you are interested, you can test it (● '? '● ))

1 // test whether gets can read a maximum of 1024 characters. 2 # include <stdio. h> 3 # include <string. h> 4 5 Int main () 6 {7 char STR [2048]; // I heard that gets can read a maximum of 1024 characters. We set an array of 2048 8 gets (STR ); // enter 9 int CNT; 10 printf ("CNT = % d", strlen (STR) from the keyboard. // The value of CNT is the number of array elements, is it greater than 1024 ??? 11 12 Return 0; 13}

I personally tested the first CNT = 2003, the second CNT = 2086, and the second program finally crashed, but it cannot be said that the gets read limit is about 2086,

The program crashes because the memory is out of bounds.

2. Let's take a closer look at the usage of fgeis. Let's take char STR [N]; fgets (STR, N, stdin); for example:

Fgets can only read N-1 characters, including the last '\ n ', after reading, the system will automatically add '\ 0' at the end (after gets finishes reading, the system will automatically replace' \ N' with '\ 0 ').

There are two situations:

1. When you enter <= N-1 characters (including '\ n') from the keyboard, STR ends with' \ n \ 0. This causes strlen (STR) to be 1 larger than you think,

Of course, you can remove '\ n' using the following code.

1 If (STR [strlen (STR)-1] = '\ n') {// remove the linefeed 2 StR [strlen (STR)-1] =' \ 0 '; 3}

2: When you enter> N-1 characters (including '\ n') from the keyboard, the STR string ends with' \ 0.

3. As we mentioned above, when the input character on the keyboard is greater than N, there are some differences between gets and fgets. Let's test it by using the following code:

1 # include <stdio. h> 2 # include <string. h> 3 # define N 5 4 5 Int main () 6 {7 char S1 [N]; 8 char S2 [N]; 9 fgets (S1, N, stdin ); 10 // gets (S1); 11 if (S1 [strlen (S1)-1] = '\ n') {// remove the linefeed 12 S1 [strlen (S1) -1] = '\ 0'; 13} 14 15 // fflush (stdin); // clear the buffer 16 fgets (S2, N, stdin ); 17 // gets (S2); 18 if (s2 [strlen (S2)-1] = '\ n') {// remove the linefeed 19 S2 [strlen (S2) -1] = '\ 0'; 20} 21 22 printf ("% S % s", S1, S2); 23 24 return 0; 25}

When we enter 12345 and press enter, the result 1234 is displayed. Is it different from what we expected? Let's just look at the result. S1 is 1234,

S2 is 5. Why?

This is the difference between fgets and gets. The first fgets reads only 1234 and puts 5' \ n' into the buffer. When the program runs to the second fgets,

It will be read directly from the buffer until '\ n' ends, so there will be such a result. In this case, S1 is 1234' \ 0', S2 is 5' \ 0 '. how should we solve this problem?

We can add fflush (stdin) in front of the second fgets. It is used to clear the buffer. You can try it and I won't be able.

In the above example, we use fgets for input. Now we can change it to gets for input. Let's see the result:

It is no longer a simple question about gets and fgets. This involves how the data we read from the keyboard is displayed in the memory. So I just want to explain it briefly.

In the memory, S1 is sorted in this order.

 

'\ 0'
6
5
4
3
2
1

When S2 is input, it will become:

'\ 0'
6
5
4
3
'\ 0'
F
E
D
C
B
A

Therefore, S1 outputs F when the output is complete, S2 outputs abcdef, and then ends with '\ 0.

 

 

 

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.