Fgets () usage notes

Source: Internet
Author: User

To avoid buffer overflow, use fgets () instead of gets () to read the input from the terminal.
However, this will also cause a problem because the call format of fgets () is:

Fgets (BUF, Max, FP)
Fgets (BUF, Max, stdin)

Buf is the name of a char array, Max is the maximum length of the string, and FP is the file pointer.
The fgets () function reads the back of the first line break it encounters, or reads a character that is less than the maximum length of the string, or to the end of the file. Then, the fgets () function adds an empty character to the end to form a string. If a line is read before the maximum number of characters is reached, it adds a line break before the null character of the string to mark the end of a line.
The problem is that sometimes there may be an extra line break at the end of the string, and we need to remove it.

#include <stdio.h>#include <string.h>#define LEN 5int main(){    char str[LEN];    fgets(str, LEN, stdin);    //fprintf(stderr, "%s %d\n", str, strlen(str));    for(int i = 0; i < LEN; i++)        printf("%d\t", str[i]);    printf("\n");    if(str[strlen(str)-1] == ‘\n‘)        str[strlen(str)-1] = ‘\0‘;    for(int i = 0; i < LEN; i++)        printf("%d\t", str[i]);    printf("\n");    return 0;}

Input:

ABC

Output:

97 98 99 10 0
97 98 99 0 0

Note: When the number of characters entered is less than the specified number, the last line break is saved at the position of S [len-1], and s [Len] is always '\ 0 '.
Input:

Abcdefg

Output:

97 98 99 100 0
97 98 99 100 0

Note: When the number of input characters is greater than the specified number, the length of the specified string is-1 characters, and the line break is not saved. The value of S [Len] is always '\ 0 '.

 

I also thought about using the following functions for processing:

void clearEnter(char *p){    while(*p)    {        if(*p == ‘\n‘)            *p = ‘\0‘;        p++;    }}

However, this function has a problem. It is effective to process the line break at the end of the string obtained by the fgets () function. If the string is not obtained from the fgets () function, and there is a line break in the string, the first line break in the string is replaced with '\ 0', and other parts of the string are discarded.

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.