fgets 和gets_C编程中的fgets()和gets()

fgets 和gets

介绍 (Introduction)

We all are familiar with the scanf() function. It is the main function applicable to take basic user inputs. Even though scanf() works great while taking inputs such as integer, character, float etc. It certainly falls behind while taking string inputs containing whitespaces. Let’s take a look at an example,

我们都熟悉scanf()函数。 它是适用于接受基本用户输入的主要功能。 即使scanf()在接受整数,字符,浮点等输入时效果很好。在接受包含空格的字符串输入时,它肯定会落后。 让我们看一个例子,


#include<stdio.h>
int main()
{
    char string[10];
    printf("Enter the string: ");
    scanf("%s", string);
    printf("\n %s",string);
    return 0;
}

Output:

输出:

Problem With Scanf
Problem With scanf()
scanf()问题

As we can observe from the above example. scanf() stops scanning as soon as it encounters whitespace or newline. This, in fact, makes taking string inputs using scanf() a bit troublesome. This can be easily avoided by using some other input functions like gets() and fgets().

从上面的示例可以看出。 scanf()遇到空格或换行符后立即停止扫描。 实际上,这使得使用scanf()获取字符串输入有些麻烦。 通过使用其他一些输入函数(例如gets()fgets() gets() ,可以轻松避免这种情况。

In this article, we are going to learn how to apply both the functions and compare them side by side.

在本文中,我们将学习如何应用这两个功能并进行比较。

C语言中的gets()函数 (gets() function in C)

gets() is a pre-defined function in C which is used to read a string or a text line. And store the input in a well-defined string variable. The function terminates its reading session as soon as it encounters a newline character.

gets()是C语言中的预定义函数,用于读取字符串或文本行。 并将输入存储在定义明确的字符串变量中。 一旦遇到换行符,该函数将终止其阅读会话。

Syntax:

句法:

gets( variable name );

gets(变量名);

The given code below illustrates the use of the gets() function,

以下给出的代码说明了gets()函数的用法,


#include<stdio.h>
int main()
{
    char string[10];
    printf("Enter the String: ");
    gets(string);
    printf("\n%s",string);
    return 0;
}

Output:

输出:

Use Of Gets()
Use Of Gets
使用获取

Compare the output with the one while using scanf(). ‘Hello World’ is now treated as a single string.

使用scanf()时将输出与输出进行比较。 “ Hello World”现在被视为单个字符串。

C中的fgets()函数 (fgets() function in C)

The standard C library also provides us with yet another function, the fgets() function. The function reads a text line or a string from the specified file or console. And then stores it to the respective string variable.

标准的C库还为我们提供了另一个函数fgets()函数。 该函数从指定的文件或控制台读取文本行或字符串。 然后将其存储到相应的字符串变量中。

Similar to the gets() function, fgets also terminates reading whenever it encounters a newline character. But furthermore, unlike gets(), the function also stops when EOF is reached or even if the string length exceeds the specified limit, n-1.

gets()函数类似,fgets在遇到换行符时也会终止读取。 但是此外,与gets()不同,该函数还会在达到EOF或即使字符串长度超过指定的限制n-1时也停止

Syntax,

句法,

fgets(char *str, int n, FILE *stream)

fgets(char * str,int n,FILE * stream)

  • str – It is the variable in which the string is going to be stored

    str –是要在其中存储字符串的变量
  • n – It is the maximum length of the string that should be read

    n –这是应读取的字符串的最大长度
  • stream – It is the filehandle, from where the string is to be read.

    –它是文件句柄,从中可以读取字符串。

Fortunately, we can both read text lines from a file or the standard input stream by using the fgets() function. Let us see how

幸运的是,我们都可以使用fgets()函数从文件或标准输入流中读取文本行。 让我们看看

1.使用fgets()从给定文件中读取 (1. Read from a given file using fgets())

For example,

例如,


#include<stdio.h>
int main()
{
    char string[20];
    FILE *fp;
    fp=fopen("file.txt","r");
    fgets(string,20,fp);
    printf("The string is: %s",string);
    fclose(fp);
    return 0;
}
    

Consider file.txt to contain the line ‘JournalDev fgets() example!’. In that case, the output of the above code would be,

考虑file.txt包含“ JournalDev fgets()示例!”这一行 。 在这种情况下,上述代码的输出为

Fgets Output
fgets() file input
fgets()文件输入

2.使用fgets()从stdin中读取 (2. Read from stdin using fgets())


#include<stdio.h>
int main()
{
    char string[20];
    printf("Enter the string: ");
    fgets(string,20,stdin);         #input from stdin stream
    printf("\nThe string is: %s",string);
    return 0;
}

Output:

输出:

Fgets() Stdin Input
fgets() Stdin Input
fgets()标准输入

结论 (Conclusion)

Even though both the functions, gets() and fgets() can be used for reading string inputs. The biggest difference between the two is the fact that the latter allows the user to specify the buffer size. Hence it is highly recommended over the gets() function.

即使这两个函数都可以使用gets()fgets()读取字符串输入。 两者之间的最大区别是后者允许用户指定缓冲区大小 。 因此,强烈建议通过gets()函数。

The gets() function doesn’t have the provision for the case if the input is larger than the buffer. As a result, memory clogging may occur. This is the part where the fgets() function shines and provides an ultimate solution.

如果输入大于缓冲区, gets()函数将不提供这种情况。 结果,可能发生内存阻塞 。 这是fgets()函数发光并提供最终解决方案的部分。

参考资料 (References)

翻译自: https://www.journaldev.com/35456/fgets-and-gets-in-c-programming

fgets 和gets

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值