本文共 4585 字,大约阅读时间需要 15 分钟。
fgets 和gets
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()
在接受整数,字符,浮点等输入时效果很好。在接受包含空格的字符串输入时,它肯定会落后。 让我们看一个例子,
#includeint main(){ char string[10]; printf("Enter the string: "); scanf("%s", string); printf("\n %s",string); return 0;}
Output:
输出:
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.
在本文中,我们将学习如何应用这两个功能并进行比较。
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()
函数的用法,
#includeint main(){ char string[10]; printf("Enter the String: "); gets(string); printf("\n%s",string); return 0;}
Output:
输出:
Compare the output with the one while using scanf()
. ‘Hello World’ is now treated as a single string.
使用scanf()
时将输出与输出进行比较。 “ Hello World”现在被视为单个字符串。
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)
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()
函数从文件或标准输入流中读取文本行。 让我们看看
For example,
例如,
#includeint 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()示例!”这一行! 。 在这种情况下,上述代码的输出为
#includeint 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:
输出:
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()
函数发光并提供最终解决方案的部分。
翻译自:
fgets 和gets
转载地址:http://zplzd.baihongyu.com/