C - 对标准输入使用 getchar 而不是 fgets
C - Using getchar over fgets for stdin
这里是新人,
使用 getchar()
方法而不是 fgets()
来读取字符串是否有任何缺点,除了 getchar()
仅从 stdin
读取的事实之外我们可以为 fgets()
.
选择一个流
假设,我必须只使用 stdin
,是否有我应该或不应该选择其中之一的原因?
我看到 getchar()
的一个优点是 fgets()
要求我们指定可以提前读取的最大字符数,而我们可以在 [=11= 的情况下使用缓冲区] 来存储字符串并根据我们的需要重新分配。
[编辑]
我实现了这个函数来读取输入,但是如果字符串的长度已知,fgets()
也足够了,有什么理由这样做。
char *scanner ( const char *text )
{
char *buffer = ( char * ) malloc ( sizeof ( char ) * BUFFER_SIZE );
int token;
int length = 0;
int limit = BUFFER_SIZE;
printf("%s", text);
while ( ( token = getchar ( ) ) != '\n' && token != EOF )
{
buffer [ length ++ ] = token;
if ( length > limit )
{
limit += 256;
buffer = ( char * ) realloc ( buffer, sizeof ( char ) * limit );
}
}
buffer [ length ] = '[=10=]';
return buffer;
}
Is there any disadvantage to using getchar()
method as opposed to
fgets()
for reading a string, other than the fact that getchar()
reads just from stdin where as we can choose a stream for fgets()
.
是的。
fgets()
一般情况下只需要调用一次,它会自动识别行尾。 getchar()
一般需要循环调用,需要自己处理行尾识别。这使得 getchar()
使用起来更复杂,为此目的成本也更高。
此外,如果您的程序中有多个地方要读取字符串,您可能希望将任何基于 getchar()
的解决方案放入 function
这样您就可以重复使用它,然后您就可以直接重新实现 fgets
。
也有理由期望编写和调整您的 C 库的专家实现 fgets()
的效率比您合理期望的 [=10] 更高=].
The one advantage I see for getchar()
is that fgets()
requires us to
specify the maximum characters that can be read in advanced, whereas
we can use a buffer in case of getchar()
to store the string and
reallocate as per our need.
如果您的意思是使用 getchar()
允许您在逐个字符的基础上重新分配 那么
- 从技术上讲,您也可以通过
fgets()
获得它。
- 您通常希望在较大的块中执行(重新)分配,因为它很昂贵。您也可以 that 与
fgets
结合使用。
这里是新人,
使用 getchar()
方法而不是 fgets()
来读取字符串是否有任何缺点,除了 getchar()
仅从 stdin
读取的事实之外我们可以为 fgets()
.
假设,我必须只使用 stdin
,是否有我应该或不应该选择其中之一的原因?
我看到 getchar()
的一个优点是 fgets()
要求我们指定可以提前读取的最大字符数,而我们可以在 [=11= 的情况下使用缓冲区] 来存储字符串并根据我们的需要重新分配。
[编辑]
我实现了这个函数来读取输入,但是如果字符串的长度已知,fgets()
也足够了,有什么理由这样做。
char *scanner ( const char *text )
{
char *buffer = ( char * ) malloc ( sizeof ( char ) * BUFFER_SIZE );
int token;
int length = 0;
int limit = BUFFER_SIZE;
printf("%s", text);
while ( ( token = getchar ( ) ) != '\n' && token != EOF )
{
buffer [ length ++ ] = token;
if ( length > limit )
{
limit += 256;
buffer = ( char * ) realloc ( buffer, sizeof ( char ) * limit );
}
}
buffer [ length ] = '[=10=]';
return buffer;
}
Is there any disadvantage to using
getchar()
method as opposed tofgets()
for reading a string, other than the fact thatgetchar()
reads just from stdin where as we can choose a stream forfgets()
.
是的。
fgets()
一般情况下只需要调用一次,它会自动识别行尾。getchar()
一般需要循环调用,需要自己处理行尾识别。这使得getchar()
使用起来更复杂,为此目的成本也更高。此外,如果您的程序中有多个地方要读取字符串,您可能希望将任何基于
getchar()
的解决方案放入function
这样您就可以重复使用它,然后您就可以直接重新实现fgets
。也有理由期望编写和调整您的 C 库的专家实现
fgets()
的效率比您合理期望的 [=10] 更高=].
The one advantage I see for
getchar()
is thatfgets()
requires us to specify the maximum characters that can be read in advanced, whereas we can use a buffer in case ofgetchar()
to store the string and reallocate as per our need.
如果您的意思是使用 getchar()
允许您在逐个字符的基础上重新分配 那么
- 从技术上讲,您也可以通过
fgets()
获得它。 - 您通常希望在较大的块中执行(重新)分配,因为它很昂贵。您也可以 that 与
fgets
结合使用。