博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
fgets 和gets_C编程中的fgets()和gets()
阅读量:2534 次
发布时间:2019-05-11

本文共 4585 字,大约阅读时间需要 15 分钟。

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
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
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
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
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)

翻译自:

fgets 和gets

转载地址:http://zplzd.baihongyu.com/

你可能感兴趣的文章
阶段3 2.Spring_02.程序间耦合_4 曾经代码中的问题分析
查看>>
阶段3 2.Spring_03.Spring的 IOC 和 DI_2 spring中的Ioc前期准备
查看>>
阶段3 2.Spring_03.Spring的 IOC 和 DI_4 ApplicationContext的三个实现类
查看>>
阶段3 2.Spring_02.程序间耦合_8 工厂模式解耦的升级版
查看>>
阶段3 2.Spring_03.Spring的 IOC 和 DI_6 spring中bean的细节之三种创建Bean对象的方式
查看>>
阶段3 2.Spring_04.Spring的常用注解_3 用于创建的Component注解
查看>>
阶段3 2.Spring_04.Spring的常用注解_2 常用IOC注解按照作用分类
查看>>
阶段3 2.Spring_09.JdbcTemplate的基本使用_5 JdbcTemplate在spring的ioc中使用
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_02.ssm整合之搭建环境
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第1节零基础快速入门SpringBoot2.0_3、快速创建SpringBoot应用之手工创建web应用...
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_04.ssm整合之编写SpringMVC框架
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第1节零基础快速入门SpringBoot2.0_5、SpringBoot2.x的依赖默认Maven版本...
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_08.ssm整合之Spring整合MyBatis框架
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_9、SpringBoot基础HTTP其他提交方法请求实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_12、SpringBoot2.x文件上传实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_19、SpringBoot个性化启动banner设置debug日志...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_20、SpringBoot2.x配置全局异常实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第5节 SpringBoot部署war项目到tomcat9和启动原理讲解_23、SpringBoot2.x启动原理概述...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_21、SpringBoot2.x配置全局异常返回自定义页面...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_32..SpringBoot2.x持久化数据方式介绍...
查看>>