The C language Gets () function and its replacement fgets () function

Source: Internet
Author: User
Tags function prototype

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

I Love BIT

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

The Get () function reads a row of data from a standard input (keyboard), and so-called reads a line, which is returned by encountering a newline character. the Get () function does not read the newline character ' \ n ', it will replace the line break with the null characters ' \ s', as a token of the end of the C-language string.

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

The Get () function has a serious flaw: it does not check that the array can be loaded with the following input lines:

Like what:

We defined an array char src[5], when we called Get (SRC) to read the string from the standard input, we see the Get () function parameter array name, we all know that the array name is equivalent to a pointer, that is, the first address of the array . At this point if our input is greater than 5 characters, such as I love Bit,gets () function will start from the address of the SRC point, followed by each character, but src allocated only 5 bytes of space, fill the five space, the Get () function will access the unallocated memory space , if the space already has data, then the program will be error, and interrupt.

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

Gets () is abandoned, then what do we use to replace its function?

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

In fact, we can use the fgets () function in C to replace the Get ()

Let's take a look at the function prototype declaration:

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

Note that the second parameter, bufsize, restricts the number of characters to be read, which solves the flaw of the Get () function.

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

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

Another thing is that thefgets () function reads the newline character (which is different from the Get function), and when the read is finished, the fgets function adds a null character to the end of the buf at the end of the string.

Can look at a simple small example:

#include <stdio.h><stdlib.h>#define LEN 6int main (int argc,Char * argv[]) {    char  Src[len];    printf ("pleaseenter:\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, first look at a set of inputs and outputs:

Input is Zhan and carriage return (' \ n '), altogether five characters, Fgets will read the five characters, and then add the end of the string at the end of the sign ' \ s ';

We know that the fputs () function does not automatically add line breaks, but the output is wrapped to output the press any ..., which means 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, fgets function is still read 5 characters (LEN-1), then fgets () read into the Zhang, is already five characters, so the carriage return line will not be read, the last fgets () to add the end of the string sign ' \ s ', So when we see the output, press any... There is no line-wrapping output, but the same line as the Zhang .

Last look at a set of inputs and outputs:

I believe we all understand it without explaining it.

A summary is:

The Get function does not limit the number of reads, which is likely to cause the program to write data to an unknown memory space and cause the program to fail.

The second parameter in the Fgets function limits the number of reads, which also solves the problem with the Get function, but note that the Fgets function only reads n-1 characters (less if a newline character is encountered), and ends with a string end flag, and Fgets also reads the line break.

The C language Gets () function and its replacement fgets () function

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.