当我使用gets()函数访问文件时,为什么GCC会警告我 [英] Why GCC does warn me when I using gets() function for accessing file

查看:77
本文介绍了当我使用gets()函数访问文件时,为什么GCC会警告我的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用gcc v.4.1.1编译源代码后,我收到一条警告

消息:

" /tmp/ccixzSIL.o:In function' 'main''; ex.c :(。text + 0x9a):警告:

''获得''功能很危险,不应该使用。

可以有人告诉我为什么get()函数很危险吗?

非常感谢。


Cuthbert


这是我测试的源代码:

--------------------------- ------------------------

/ * count.c - 使用标准I / O * /


#include" stdafx.h"

#include< stdio.h>

#include< stdlib.h // ANSI C exit()原型

int main(int argc,char * argv [])

{

int ch; //将每个字符存储为读取的位置

FILE * fp; //文件指针

长计数= 0;


if(argc!= 2)

{

printf(" Usage:%s filename \\\
",argv [0]);

exit(1);

}

if((fp = fopen(argv [1]," r"))== NULL)

{

printf(" Can 打开%s \ n,argv [1]);

退出(1);

}

while(( ch = getc(fp))!= EOF)

{

putc(ch,stdout); //与putchar(ch)相同;

count ++;

}

fclose(fp);

printf (文件%s有%ld个字符\ n,argv [1],计数);


返回0;

}

--------------------------------------------- ---------------------

After compiling the source code with gcc v.4.1.1, I got a warning
message:
"/tmp/ccixzSIL.o: In function ''main'';ex.c: (.text+0x9a): warning: the
''gets'' function is dangerous and should not be used."

Could anybody tell me why gets() function is dangerous??
Thank you very much.

Cuthbert

Here is the source code I was testing:
---------------------------------------------------
/* count.c -- using standard I/O */

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h// ANSI C exit() prototype

int main(int argc, char *argv[])
{
int ch; // place to store each character as read
FILE *fp; // "file pointer"
long count = 0;

if (argc != 2)
{
printf("Usage: %s filename\n", argv[0]);
exit(1);
}
if ((fp = fopen(argv[1], "r")) == NULL)
{
printf("Can''t open %s\n", argv[1]);
exit(1);
}
while ((ch = getc(fp)) != EOF)
{
putc(ch,stdout); // same as putchar(ch);
count++;
}
fclose(fp);
printf("File %s has %ld characters\n", argv[1], count);

return 0;
}
------------------------------------------------------------------

推荐答案

Cuthbert写道:
Cuthbert wrote:

用gcc v.4.1.1编译源代码后,我收到一条警告

消息:

" /tmp/ccixzSIL.o:在函数''main''; ex.c :(。text + 0x9a):警告:

''获取''函数是危险的,不应该可以使用。


有人可以告诉我为什么get()函数很危险吗?

非常感谢你。

Cuthbert


这是我测试的源代码:

--------------- ------------------------------------

/ * count.c - - 使用标准I / O * /

#include&quo t; stdafx.h"

#include< stdio.h>

#include< stdlib.h // ANSI C exit()原型


int main(int argc,char * argv [])

{

int ch; //将每个字符存储为读取的位置

FILE * fp; //文件指针

长计数= 0;


if(argc!= 2)

{

printf(" Usage:%s filename \\\
",argv [0]);

exit(1);

}

if((fp = fopen(argv [1]," r"))== NULL)

{

printf(" Can 打开%s \ n,argv [1]);

退出(1);

}

while(( ch = getc(fp))!= EOF)

{

putc(ch,stdout); //与putchar(ch)相同;

count ++;

}

fclose(fp);

printf (文件%s有%ld个字符\ n,argv [1],计数);


返回0;

}

--------------------------------------------- ---------------------
After compiling the source code with gcc v.4.1.1, I got a warning
message:
"/tmp/ccixzSIL.o: In function ''main'';ex.c: (.text+0x9a): warning: the
''gets'' function is dangerous and should not be used."

Could anybody tell me why gets() function is dangerous??
Thank you very much.

Cuthbert

Here is the source code I was testing:
---------------------------------------------------
/* count.c -- using standard I/O */

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h// ANSI C exit() prototype

int main(int argc, char *argv[])
{
int ch; // place to store each character as read
FILE *fp; // "file pointer"
long count = 0;

if (argc != 2)
{
printf("Usage: %s filename\n", argv[0]);
exit(1);
}
if ((fp = fopen(argv[1], "r")) == NULL)
{
printf("Can''t open %s\n", argv[1]);
exit(1);
}
while ((ch = getc(fp)) != EOF)
{
putc(ch,stdout); // same as putchar(ch);
count++;
}
fclose(fp);
printf("File %s has %ld characters\n", argv[1], count);

return 0;
}
------------------------------------------------------------------



这个函数很危险,因为你无法传递

它是给定缓冲区的大小。


这意味着如果任何输入大于你的缓冲区,你将获得
将有严重的后果,可能是一次崩溃。


你可以使用几种替代品,其中一种是由新闻组成员Chuck Falconer开发的。\\ b


http://cbfalconer.home.att.net/download/

This function is dangerous because there is no way you can pass
it the size of the given buffer.

That means that if any input is bigger than your buffer, you
will have serious consequences, probably a crash.

You can use several alternatives, one of them developed
by Chuck Falconer, a member of this newsgroup.

http://cbfalconer.home.att.net/download/


非常感谢。


BTW,有什么方法可以知道有多大是我的输入缓冲区?


Cuthbert

jacob navia写道:
Thank you very much.

BTW, is there any method to know how big is my input buffer?

Cuthbert
jacob navia wrote:

Cuthbert写道:
Cuthbert wrote:

用gcc v.4.1.1编译源代码后,我收到一条警告

消息:

" /tmp/ccixzSIL.o :在函数''main''; ex.c :(。text + 0x9a):警告:

''获取''函数是危险的,不应该使用。


有人可以告诉我为什么get()函数很危险吗?

非常感谢你。


Cuthbert


这是我测试的源代码:

----------------------- ----------------------------

/ * count.c - 使用标准I / O * /


#include" stdafx.h"

#include< stdio.h>

#include< stdlib。 h // ANSI C exit()原型

int main(int argc,char * argv [])

{

int ch; //将每个字符存储为读取的位置

FILE * fp; //文件指针

长计数= 0;


if(argc!= 2)

{

printf(" Usage:%s filename \\\
",argv [0]);

exit(1);

}

if((fp = fopen(argv [1]," r"))== NULL)

{

printf(" Can 打开%s \ n,argv [1]);

退出(1);

}

while(( ch = getc(fp))!= EOF)

{

putc(ch,stdout); //与putchar(ch)相同;

count ++;

}

fclose(fp);

printf (文件%s有%ld个字符\ n,argv [1],计数);


返回0;

}

--------------------------------------------- ---------------------
After compiling the source code with gcc v.4.1.1, I got a warning
message:
"/tmp/ccixzSIL.o: In function ''main'';ex.c: (.text+0x9a): warning: the
''gets'' function is dangerous and should not be used."

Could anybody tell me why gets() function is dangerous??
Thank you very much.

Cuthbert

Here is the source code I was testing:
---------------------------------------------------
/* count.c -- using standard I/O */

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h// ANSI C exit() prototype

int main(int argc, char *argv[])
{
int ch; // place to store each character as read
FILE *fp; // "file pointer"
long count = 0;

if (argc != 2)
{
printf("Usage: %s filename\n", argv[0]);
exit(1);
}
if ((fp = fopen(argv[1], "r")) == NULL)
{
printf("Can''t open %s\n", argv[1]);
exit(1);
}
while ((ch = getc(fp)) != EOF)
{
putc(ch,stdout); // same as putchar(ch);
count++;
}
fclose(fp);
printf("File %s has %ld characters\n", argv[1], count);

return 0;
}
------------------------------------------------------------------



此功能很危险,因为你无法通过

它是给定缓冲区的大小。


这意味着如果任何输入大于你的缓冲区,你将获得
将有严重的后果,可能是一次崩溃。


你可以使用几种替代品,其中一种是由新闻组成员Chuck Falconer开发的。\\ b


http://cbfalconer.home.att.net/download/


" Cuthbert" < cu ********** @ gmail.comwrites:
"Cuthbert" <cu**********@gmail.comwrites:

非常感谢。


BTW,有什么方法可以知道我的输入缓冲区有多大吗?
Thank you very much.

BTW, is there any method to know how big is my input buffer?



请不要顶一下。我已经剪断了其余的背景,因为它不是特别相关的b $ b。 Cuthbert感谢Jacob Navia

解释了get()的危险。


要回答你的新问题,正确的方法是:

fgets(buffer,sizeof buffer,stdin);


如果你已经将缓冲区定义为char数组,你可以使用该行作为

是。如果buffer是一个指针,你必须自己弄清楚sizeof buffer




如果缓冲区溢出,fgets ()将返回适合

缓冲区的内容。判断是否是这种情况的一种方法是成功地使用
fgets()返回以''\ n''结尾的字符串。如果你检查结束

你的字符串并且它缺少''\ n'',你就没有得到所有的输入,

和你需要再次运行fgets()(可能还有一个新的缓冲区)

来完成剩下的工作。

希望我解释得那么好。


-

Andrew Poelstra< http://www.wpsoftware.net/projects>

要通过电子邮件与我联系,请使用apoelstra以上域名。

电缆的末端是否需要插入? -Anon。

Please don''t top-post. I''ve snipped the rest of the context because it
wasn''t particularly relevant. Cuthbert was thanking Jacob Navia for
explaining the dangers of gets().

To answer your new question, the proper method is:
fgets (buffer, sizeof buffer, stdin);

If you''ve defined buffer as an array of char, you can use that line as
is. If buffer is a pointer, you''ll have to figure out "sizeof buffer"
on your own.

If the buffer is overrun, fgets() will return what could fit in the
buffer. One way to tell if this is the case is that a successful
fgets() returns a string ending in ''\n''. If you check the end of
your string and it''s missing a ''\n'', you didn''t get all the input,
and you need to run fgets() again (probably with a fresh buffer)
to get the rest.
Hope I explained that well.

--
Andrew Poelstra <http://www.wpsoftware.net/projects>
To reach me by email, use `apoelstra'' at the above domain.
"Do BOTH ends of the cable need to be plugged in?" -Anon.


这篇关于当我使用gets()函数访问文件时,为什么GCC会警告我的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆