如何sscanf只返回整数 [英] How to sscanf return integer only

查看:109
本文介绍了如何sscanf只返回整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:


sscanf(line,"%d",n_ptr)!= 1 || n_ptr< = 0;


它只是部分有效。如果用户键入0-9以外的字符到

,则启动字符串失败。但是只要第一个字符是

整数,它就会允许以下地方的字母


例如:


H78(按预期失败)

78(按预期工作)

7H8(并没有按需要失败)


解决方案

2006-05-05,Yogi_Bear_79< no **** @ spamsux.com>写道:

我有以下代码:

sscanf(line,"%d",n_ptr)!= 1 || n_ptr< = 0;

它只是部分有效。如果用户键入0-9以外的字符来启动字符串则会失败。但是,只要第一个字符是
整数,它就会允许以下地方的字母

例如:

H78(按预期失败)
78(按预期工作)
7H8(并没有按需要失败)




但是_is_是整数 - 7.然后是整数后的东西。


和'是'|| n_ptr< = 0"应该怎么做?


但是_is_是一个整数 - 7.然后是整数之后的东西。


如果除了整数或白色之外还有任何其他字符需要失败

空间


什么' 's" || n_ptr< = 0"应该做什么?




不需要,删除,相同的结果


2006年5月4日星期四20: 23:56 -0400,Yogi_Bear_79 < no **** @ spamsux.com>

写道:

我有以下代码:

sscanf( line,%d,n_ptr)!= 1 || n_ptr< = 0;


第三个参数需要&但我认为这是一个错字。


如果你想知道线上的数据是否是格式良好

整数,请使用strtol会告诉你它停止提取

数据的位置。然后检查该空格的char或''\ 0''。

它只能部分工作。如果用户键入0-9以外的字符来启动字符串则会失败。但是,只要第一个字符是
整数,它就会允许以下地方的字母

例如:

H78(按预期失败)
78(按预期工作)
7H8(并没有按需要失败)



删除电子邮件的del


I have the following code:

sscanf(line, "%d", n_ptr) !=1 || n_ptr <=0;

It only partially works. If the user types a character other than 0-9 to
start the string it fails. However as long as the first character is an
integer it will allow letters in the following places

for Example:

H78 (fails as expected)
78 (works as expected)
7H8 ( doesn''t fail as desired)


解决方案

On 2006-05-05, Yogi_Bear_79 <no****@spamsux.com> wrote:

I have the following code:

sscanf(line, "%d", n_ptr) !=1 || n_ptr <=0;

It only partially works. If the user types a character other than 0-9 to
start the string it fails. However as long as the first character is an
integer it will allow letters in the following places

for Example:

H78 (fails as expected)
78 (works as expected)
7H8 ( doesn''t fail as desired)



But there _is_ an integer - 7. there''s then stuff after the integer.

and what''s "|| n_ptr <=0" supposed to do?


But there _is_ an integer - 7. there''s then stuff after the integer.
It needs to fail if there are any characters other than integers or white
space

and what''s "|| n_ptr <=0" supposed to do?



Not needed, removed, same results


On Thu, 4 May 2006 20:23:56 -0400, "Yogi_Bear_79" <no****@spamsux.com>
wrote:

I have the following code:

sscanf(line, "%d", n_ptr) !=1 || n_ptr <=0;
The third argument needs an & but I assume that is a typo.

If you want to know whether the data at line is a "well formed"
integer, use strtol which will tell you where it stopped extracting
data. Then examine that char for white space or ''\0''.

It only partially works. If the user types a character other than 0-9 to
start the string it fails. However as long as the first character is an
integer it will allow letters in the following places

for Example:

H78 (fails as expected)
78 (works as expected)
7H8 ( doesn''t fail as desired)


Remove del for email


这篇关于如何sscanf只返回整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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