Detailed C language gets () function and its substitute fgets () function _c language

Source: Internet
Author: User
Tags function prototype stdin

There are several ways to read a string in C, such as scanf () with%s, but this method can only get one word, which means that a null character, such as a space, is returned. If you want to read a line of strings, for example:

I Love BIT

In this case, scanf () is powerless. The first thing we thought of was reading with gets ().

The gets () function reads a row of data from the standard input (the keyboard), which is called to read a line, and is returned when a newline character is encountered. The gets () function does not read the newline character ' \ n ', it will replace the newline character with the empty characters ' "," as the C language string end of the flag.

The gets () function is often paired with the puts () function, and the puts () function is used to display the string and automatically add a newline flag after the string ' \ n '.

There is a serious flaw in the gets () function: It does not check that the array can be loaded with the following input line:

Like what:

We have defined an array char src[5], at which point we call gets (SRC) to read the string from the standard input, and we see that the gets () function has an array name, which we all know is the equivalent of a pointer, the first address of an array. At this point, if our input is greater than 5 characters, for example, I love Bit,gets () function will start from the address of SRC, fill in each character, but src only allocated 5 bytes of space, fill the five space, the gets () function will access the unallocated memory space, If this space already contains data, then the program will be wrong, and interrupt.

Formally due to this flaw in the gets () function, the gets () function is no longer recommended in the C99 standard, and the function is discarded directly in C11.

Gets () is discarded, so what do we use to replace its function?

The C11 standard adds the gets_s () function to replace the gets () function, but the function is an optional extension of the stdio.h input-output function system class, so even if the compiler supports the C11 standard, the gets_s () function may not be supported.

In fact, we can use the C language fgets () function to replace gets ()

Let's look at the function prototype declaration first:

Char *fgets (char *buf, int bufsize, FILE *stream);

Notice the second parameter bufsize, which limits the number of characters read, which solves the flaw of the gets () function.

We know that the fgets () function is primarily used to read files, and if you want to read the keyboard, the stream parameter should be stdin.

Note that if the bufsize is set to N, then the fgets () function reads up to n-1 characters, and the word "maximum" is used because the Fgets function returns if a newline character has been encountered before.

Also, the fgets () function reads the newline character (which is different from the gets function), and when the read is finished, the fgets function adds a null character to the end of the BUF as the end of the string.

You can look at a simple little example:

#include <stdio.h>
#include <stdlib.h>
#define LEN 6
int main (int argc,char* argv[])
{
Char Src[len];
printf ("Please enter:\n");
Fgets (Src,len,stdin);
printf ("Your Enter is:\n");
Fputs (src,stdout);
}

In this program, I set the length of the array to 6, looking at a set of inputs and outputs first:

Enter as Zhan and carriage return (' \ n '), a total of five characters, Fgets will read the five characters, and then add the end of the string at the end of the tag ' ";

We know that the fputs () function does not automatically add line breaks, but the output results in a newline output of press any ..., which shows that the fgets () function reads the newline character.

Looking at a set of input and output:

This time I entered the Zhang and carriage return line, the Fgets function is still read 5 characters (LEN-1), then fgets () read into Zhang, is already five characters, so the carriage return line will not read, the last Fgets () Add string End Flag ' "," So when we see the output, press any ... There is no line output, but with Zhang in the same line.

Finally, look at a set of inputs and outputs:

I'm sure we all understand it without explanation.

The summary is:

The gets function does not limit the number of reads, which can cause programs to write data to unknown memory space, causing program errors.

The second parameter in the Fgets function restricts the number of reads. This also solves the problem with the gets function, but note that the Fgets function reads only n-1 characters (less if a newline character is encountered), and then adds a string end flag at the end, and Fgets also reads the line feed.

The above is a small set to introduce the details of the C language gets () function and its replacement fgets () function of the relevant knowledge, hope to help everyone, if you want to know more, please pay attention to cloud habitat community!

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.