您的位置:首页 > 编程语言 > C语言/C++

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() return
a 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++