C-language gets function, use of fgets function

Source: Internet
Author: User
Tags stdin

Gets the string function read from the standard input device. Can be read indefinitely without judging the upper limit to return to the end of the read. The function's specific functions are as follows: reads a string from the stdin stream until it is accepted to a newline or EOF, and stores the result of the read in the character array pointed to by the buffer pointer. The newline character is not read as the contents of the string, and the read line break is converted to the ' \ s ' null and thus ends the string.

You should pay attention to the following features when using the Get function: You can read indefinitely without judging the upper limit, so the programmer should make sure that buffer space is large enough so that no overflow occurs while the read operation is performed. If overflow, the extra character is written to the stack, destroying the value of one or more unrelated variables. This fact causes the GET function to only apply to the toy program, in order to avoid this situation, we can use Fgets (stdin) to read the standard input to replace the get (). For backwards compatibility, gets removes line breaks, and gets does not buffer the newline character.

The 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. The prototype is Char *fgets (char *s, int n, FILE *stream);

Also note the considerations when mixing the scanf function with the gets function:

1, get in scanf before the call, this call generally do not have any problems, you can enter the normal.

2, scanf is called before get, this situation will be a problem, when the variables in the scanf, run to the Get function, do not let enter any characters, this problem occurs because the Get function received the scanf after the input return character, The solution is to use the GetChar function to receive the carriage return in the middle of scanf and get, so there is no problem.

C-language gets function, use of fgets function

Related Article

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.