获得有关获取的警告 [英] getting a warning about gets
问题描述
我正在对我的linux gcc编译器发出警告:
/tmp/ccXgHa9s.o(.text+0x48):在函数`main'':
:警告:'gets''函数很危险,不应该使用。
这里我使用的地方得到:
#include" common.h" //common.h包含string.h
....
....
int main(void)
{
char zf_name [50];
....
printf(" ;输入文件的名称\ n");
得到(zf_name);
...
返回0;
}
I''m getting a warning on my linux gcc compiler:
/tmp/ccXgHa9s.o(.text+0x48): In function `main'':
: warning: the `gets'' function is dangerous and should not be used.
And here''s where I used gets:
#include "common.h" //common.h includes string.h
....
....
int main(void)
{
char zf_name[50];
....
printf("Enter the name of the file\n");
gets(zf_name);
...
return 0;
}
推荐答案
5月19日,10:38 * am,pereges< Brol ... @ gmail.comwrote:
On May 19, 10:38*am, pereges <Brol...@gmail.comwrote:
我在linux gcc编译器上收到警告:
/tmp/ccXgHa9s.o(.text + 0x48):在函数main中:
:警告:`gets''函数很危险,不应该使用。
这里我用过的地方得到了:
#include" common.h" * // common.h包含string.h
...
...
int main(void)< br $>
{
* * char zf_name [50];
* * ....
* * printf(输入文件的名称\ n);
* *得到(zf_name);
* * ...
* *返回0;
} - 隐藏引用的文字 -
- 显示引用的文字 -
I''m getting a warning on my linux gcc compiler:
/tmp/ccXgHa9s.o(.text+0x48): In function `main'':
: warning: the `gets'' function is dangerous and should not be used.
And here''s where I used gets:
#include "common.h" *//common.h includes string.h
...
...
int main(void)
{
* *char zf_name[50];
* *....
* *printf("Enter the name of the file\n");
* *gets(zf_name);
* *...
* *return 0;
}- Hide quoted text -
- Show quoted text -
我认为这个消息很明确; gets()是危险的,并且
不应该被使用。
或者,使用fgets()或者scanf()来获取输入
-
包括
I think the message is rather explicit; gets() is dangerous, and
shouldn''t be used.
Alternatively, use fgets() or perhaps, scanf() to get input
--
include
嗨
5月19日上午11点38分,pereges< Brol ... @ gmail.comwrote:
Hi
On May 19, 11:38 am, pereges <Brol...@gmail.comwrote:
:警告:获取功能很危险,应该不用。
char zf_name [50];
得到(zf_name);
: warning: the `gets'' function is dangerous and should not be used.
char zf_name[50];
gets(zf_name);
你怎么知道文件名少于50个字符?
使用:
char zf_name [SOME_CONSTANT];
fgets(zf_name,SOME_CONSTANT,stdin);
这至少不会溢出缓冲区。
您还应该这样做:
if(ferror(stdin))/ *有读错误* /
if(zf_name [strlen(zf_name) - 1] ==''\ n'')
zf_name [strlen(zf_name) - 1] = 0; / *取换行字符
关闭文件名* /
其他
/ *行比SOME_CONSTANT长,部分它缺少* /
,也许还有:
if(feof(stdin))/ *文件结束时间* /
HTH
viza
How do you know the file name is less than 50 characters?
Use:
char zf_name[ SOME_CONSTANT ];
fgets( zf_name, SOME_CONSTANT, stdin );
This will at least not overflow your buffer.
You should also do:
if( ferror( stdin )) /* there was a read error */
if( zf_name[ strlen(zf_name) - 1 ] == ''\n'' )
zf_name[ strlen(zf_name) - 1 ] = 0; /* take the newline character
off the filename */
else
/* line was longer than SOME_CONSTANT, and part of it is missing */
and maybe also:
if( feof( stdin )) /* end of file was reached */
HTH
viza
5月19日下午1:54,viza< tom.v ... @ gmail。 comwrote:
On May 19, 1:54 pm, viza <tom.v...@gmail.comwrote:
嗨
5月19日上午11点38分,pereges< Brol ... @ gmail。 comwrote:
Hi
On May 19, 11:38 am, pereges <Brol...@gmail.comwrote:
:警告:'gets''函数很危险,不应该使用。
char zf_name [50];
得到(zf_name);
: warning: the `gets'' function is dangerous and should not be used.
char zf_name[50];
gets(zf_name);
你怎么知道文件名少于50个字符?
使用:
char zf_name [SOME_CONSTANT];
fgets(zf_name,SOME_CONSTANT,stdin);
这至少不会溢出缓冲区。
您还应该这样做:
if(ferror(stdin))/ *有读错误* /
How do you know the file name is less than 50 characters?
Use:
char zf_name[ SOME_CONSTANT ];
fgets( zf_name, SOME_CONSTANT, stdin );
This will at least not overflow your buffer.
You should also do:
if( ferror( stdin )) /* there was a read error */
并不一定意味着有读错误。可能已经达到了EOF。
if(!feof(stdin)&& ferror(stdin))意味着发生了错误。
Does not necessarily mean there was a read error. It could have been
that EOF was reached.
if(!feof(stdin) && ferror(stdin)) means an error happened.
if(zf_name [strlen(zf_name) - 1] ==''\ n'')
if( zf_name[ strlen(zf_name) - 1 ] == ''\n'' )
首先检查strlen(zf_name)0
First check that strlen(zf_name) 0
zf_name [strlen(zf_name) - 1] = 0; / *取换行字符
关闭文件名* /
其他
/ *行比SOME_CONSTANT长,部分缺少* /
zf_name[ strlen(zf_name) - 1 ] = 0; /* take the newline character
off the filename */
else
/* line was longer than SOME_CONSTANT, and part of it is missing */
不一定。大部分时间都是这意味着什么。如果没有达到EOF
,你可以尝试用getc从stdin读取一个字节。如果
getc返回EOF,请检查流是否为feof()。如果feof()返回
正值,那么该行不会超过SOME_CONSTANT(除非
ferror()返回正数)。如果getc()没有返回EOF,则取消
返回值,在这种情况下是;该行长于
SOME_CONSTANT。
Not necessarily again. Most of the times that''s what it means. If EOF
wasn''t reached, you can try to read a byte from stdin with getc. If
getc returns EOF, check the stream if feof(). If feof() returns a
positive value, then the line wasn''t longer than SOME_CONSTANT (unless
ferror() returns positive). If getc() does not return EOF, ungetc the
return value, and in that case yes; the line was longer than
SOME_CONSTANT.
这篇关于获得有关获取的警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!