fgets和gets的区别
2016-03-30 16:06
330 查看
fgets()和gets() 用来读取一行
原链接:点击打开链接Read a string from console or file
Prototypes
#include <stdio.h> char *fgets(char *s, int size, FILE *stream); char *gets(char *s);
Description
These are functions that will retrieve a newline-terminated string from the console or a file. In other normal words, it reads a line of text. The behavior is slightly different, and,as such, so is the usage. For instance, here is the usage of gets():
Don't use gets().
Admittedly, rationale would be useful, yes? For one thing, gets() doesn't
allow you to specify the length of the buffer to store the string in. This would allow people to keep entering data past the end of your buffer, and believe me, this would be Bad News.
I was going to add another reason, but that's basically the primary and only reason not to use gets().
As you might suspect, fgets() allows you to specify a maximum string length.
One difference here between the two functions: gets() will devour and throw
away the newline at the end of the line, while fgets() will store it at the end of your string (space permitting).
Here's an example of using fgets() from the console, making it behave more
like gets():
char s[100]; gets(s); // read a line (from stdin) fgets(s, sizeof(s), stdin); // read a line from stdin
In this case, the sizeof() operator gives us the total size of the array in bytes, and
since a char is a byte, it conveniently gives us the total size of the array.
Of course, like I keep saying, the string returned from fgets() probably
has a newline at the end that you might not want. You can write a short function to chop the newline off, like so:
char *remove_newline(char *s) { int len = strlen(s); if (len > 0 && s[len-1] == '\n') // if there's a newline s[len-1] = '\0'; // truncate the string return s; }
So, in summary, use fgets() to read a line of text from the keyboard or
a file, and don't use gets().
Return Value
Both fgets() and fgets() returna pointer to the string passed.
On error or end-of-file, the functions return NULL.
Example
char s[100]; gets(s); // read from standard input (don't use this--use fgets()!) fgets(s, sizeof(s), stdin); // read 100 bytes from standard input fp = fopen("datafile.dat", "r"); // (you should error-check this) fgets(s, 100, fp); // read 100 bytes from the file datafile.dat fclose(fp); fgets(s, 20, stdin); // read a maximum of 20 bytes from stdin
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 关于指针的一些事情
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- Lua中调用C++函数示例
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- C++联合体转换成C#结构的实现方法
- C++高级程序员成长之路
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题
- C++变位词问题分析
- C/C++数据对齐详细解析
- C++基于栈实现铁轨问题
- C++中引用的使用总结
- 使用Lua来扩展C++程序的方法
- C++中调用Lua函数实例
- Lua和C++的通信流程代码实例
- C与C++之间相互调用实例方法讲解
- 解析C++中派生的概念以及派生类成员的访问属性