警告Scanf [英] Warning against Scanf

查看:97
本文介绍了警告Scanf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在阅读O''Reilly'的'Practical C programming"本书并且它警告

反对使用scanf,建议避免完全使用它。

相反,它建议使用fgets和sscanf。但是没有提供

的解释,除了那个扫描线的结尾非常好。

非常糟糕。我在做一些数字编程时遇到过这样的问题,但从来没有理解过。像某些消极的东西

scanfs不能正确读取值。


有些人可以给我一个关于这个问题的指针,特别是如何

安全使用scanf。


真诚地

b托马斯

解决方案

B Thomas写道:


我正在阅读O''Reilly'的'Practical C programming"本书并警告不要使用scanf,建议避免完全使用它。


我同意这个建议,虽然肯定*可以*安全地使用scanf



相反,它建议使用使用fgets和sscanf。


我甚至不会使用sscanf,坦率地说。

但是没有提供
解释,除了那个scanf handels行尾非常糟糕。


相反,处理它们很棘手。

我在做一些数字编程时遇到过这样的问题,但从来没有理解过。像某些确定的扫描文件不能正确读取值。

有些人可以给我一个关于这个问题的指针,特别是如何安全地使用scanf。



我想,Dan Pop是你的男人。 IIRC他是这个

新闻组的唯一定期撰稿人,他认为使用scanf是个好主意。


-

Richard Heathfield: bi****@eton.powernet.co.uk

Usenet是一个奇怪的地方。 - Dennis M Ritchie,1999年7月29日。

C FAQ: http://www.eskimo.com/~scs/C-faq/top.html

K& R答案,C书等:< a rel =nofollowhref =http://users.powernet.co.uk/etontarget =_ blank> http://users.powernet.co.uk/eton


Richard Heathfield写道:


B Thomas写道:


我正在阅读O''Reilly'的Practical C programming。本书并警告不要使用scanf,建议避免使用它。



我同意这个建议,尽管*使用scanf肯定是*可能*安全。

相反,它建议使用fgets和sscanf。



我甚至不会使用sscanf,坦率地说。

然而,除了那个扫描线以外,没有提供
解释非常糟糕。



更确切地说,处理它们很棘手。

我在做一些数字编程时遇到过这样的问题但却从未理解过。像某些连续的
scanfs这样的东西不能正确读取值。

有些人可以给我一个关于这个问题的指针,特别是如何安全地使用scanf。


我认为Dan Pop是你的男人。
IIRC他是这个
新闻组的唯一定期撰稿人,他认为使用scanf是一个好主意。



我已经在某人的家庭作业上实施了Dan'的技巧,

一次或两次。

http://groups.google.com/groups? selm ... mindspring.com


我喜欢它。


他也有使用feof的变体。

http:/ /groups.google.com/groups?selm...unnews.cern.ch


-

pete



" B Thomas" <第***** @ math.ohio-state.edu>在消息中写道


有些人可以给我一个关于这个问题的指针,特别是如何安全地使用scanf。



scanf("%s",str)是伪装的gets()。


但是scanf()确实有自己的微型格式化语言

允许你分析字符串的最大宽度,如何处理

溢出等等。

我不知道这种格式化语言(尽管我很少从文字

文件)。你工作的很多程序员很可能也不会熟悉它。


Hi,
I was reading O''Reilly''s "Practical C programming" book and it warns
against the use of scanf, suggesting to avoid using it completely .
Instead it recomends to use using fgets and sscanf. However no
explanation is offered other than that scanf handels end of lines very
badly. I have exeperienced such problems when doing some numerical
programming but never understood it. Things like some consequitive
scanfs would not read in values correctly.

Could some please give me a pointer on this issue, in particular how to
use scanf safely.

sincerely
b thomas

解决方案

B Thomas wrote:

Hi,
I was reading O''Reilly''s "Practical C programming" book and it warns
against the use of scanf, suggesting to avoid using it completely .
I agree with that advice, although it is certainly *possible* to use scanf
safely.
Instead it recomends to use using fgets and sscanf.
I wouldn''t even use sscanf, to be frank.
However no
explanation is offered other than that scanf handels end of lines very
badly.
Rather, handling them well is tricky.
I have exeperienced such problems when doing some numerical
programming but never understood it. Things like some consequitive
scanfs would not read in values correctly.

Could some please give me a pointer on this issue, in particular how to
use scanf safely.



Dan Pop is your man, I think. IIRC he''s the only regular contributor to this
newsgroup who thinks using scanf is a good idea.

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton


Richard Heathfield wrote:


B Thomas wrote:

Hi,
I was reading O''Reilly''s "Practical C programming" book and it warns
against the use of scanf, suggesting to avoid using it completely .



I agree with that advice, although it is certainly *possible* to use scanf
safely.

Instead it recomends to use using fgets and sscanf.



I wouldn''t even use sscanf, to be frank.

However no
explanation is offered other than that scanf handels end of lines very
badly.



Rather, handling them well is tricky.

I have exeperienced such problems when doing some numerical
programming but never understood it. Things like some consequitive
scanfs would not read in values correctly.

Could some please give me a pointer on this issue,
in particular how to use scanf safely.



Dan Pop is your man, I think.
IIRC he''s the only regular contributor to this
newsgroup who thinks using scanf is a good idea.



I''ve implemented Dan''s technique on somebody''s homework,
once or twice.

http://groups.google.com/groups?selm...mindspring.com

I like it.

He also has a variation using feof.

http://groups.google.com/groups?selm...unnews.cern.ch

--
pete



"B Thomas" <th*****@math.ohio-state.edu> wrote in message


Could some please give me a pointer on this issue, in particular how to
use scanf safely.


scanf("%s", str) is gets() in disguise.

However scanf() does have its own minature formatting language that will
allow you to sepcify the maximum width of the string, how to handle
overflow, etc.
I don''t know this formatting language (though I seldom read fields from text
files). It''s likely that many of the programmers you work with won''t be
familiar with it either.


这篇关于警告Scanf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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