程序员想问一下关于sscanf的问题 [英] Programmer wannabee question about sscanf

查看:64
本文介绍了程序员想问一下关于sscanf的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

sscanf比scanf有什么优势?

我有以下代码:


#include< stdio.h>

#include< string.h>


int main(无效)

{

int开始,结束,价值观;

char line [10];


memset(第0,10行);


printf(事件编号?);

fgets(line,sizeof(line),stdin);

values = sscanf(line," %d%d",& start,& finish);


printf(" start =%d en finish =%d values =%d \ n",

开始,结束,价值观);


返回0;

}


但如果用户输入例如:erte 5 6 ..开始输出

并且完成不会是5或6但是有些奇怪的价值...


我以为sscanf会找到整数值并将这些整数值分配给变量吗?


如果不是我怎么能完成t他的?

What advantages does sscanf offer over scanf?

I had the following code:

#include <stdio.h>
#include <string.h>

int main(void)
{
int start, finish, values;
char line[10];

memset(line,0, 10);

printf("Event numbers ? ");
fgets(line, sizeof(line), stdin);
values = sscanf(line,"%d %d", &start, &finish);

printf("start = %d en finish = %d values = %d\n",
start, finish, values);

return 0;
}

but if tha user inputted for example : erte 5 6 ..the output of start
and finished would not be 5 or 6 but some strange value...

I thought that sscanf would find values that are integers and assign
those integer values to the variables?

If not how can I accomplish this?

推荐答案

sscanf不会超过scanf;它只是从

字符串而不是标准输入中获取输入。 scanf和sscanf都非常严格;他们希望使用printf完全由

创建的输入完全相同的格式化字符串。



string是循环遍历字符串,逐个字符,测试每个字符
以查看它是否为数字。然后,当到达一个数字时

(如果你愿意,你可以要求并检查数字是否为

字符串中的第一个字符或前面有空格)使用strtol

将以该数字开头的数字转换为字符串。

strtol将指针作为其第二个参数,并将指针

到第一个字符之后的数字变成了变量指向

那个第二个参数。它返回数字。

然后你继续循环遍历字符串,直到你达到另一个

数字,之后你可以再次调用strtol!

一般来说,最好编写自己的代码来解析输入字符串,而不是使用scanf。

我注意到你在代码中使用memset为零你的缓冲区

在读入输入之前。代码将同样好用,但没有

它,但我不知道这个代码的真实上下文,所以也许

它是有效的。

此外,您对用户的

输入设置了一个10个字符的人为限制。如果用户想要定义两个五位数的
数字之间的范围,则它将不适合,并且第二个数字的结尾将被切断为b / b
。我通常使用我编写的函数将整个

行读入动态分配的内存,这可以扩展为需要

,但是将缓冲区大小增加到256会做还有这个

的情况。

我写的从标准输入读取行的函数可以在
nofollowhref =http://www.nic-nac-project.de/~baseball/code/readline.c; target =_ blank> http://www.nic-nac-project.de/~baseb...de/readline.c;


随时可以在你的代码。

我希望这一切都有帮助!

Jimmy Hartzell

sscanf does no more than scanf does; it just gets its input from a
string rather than from standard input. Both scanf and sscanf are
extremely strict; they expect input that would have been created by the
exact same formatting string using printf.
The most straight-forward way to get the first two numbers from a
string is to loop through the string, character by character, testing
each character to see if it is a digit. Then, when a digit is reached
(and you can, if you want, require and check that the digit either be
the first character in the string or preceded by whitespace) use strtol
to convert the number starting with that digit into the string.
strtol takes a pointer as its second argument, and will put a pointer
to the first character after the number into the variable pointed to by
that second argument. It returns the number.
You then continue to loop through the string until you reach another
digit, after which you can call strtol again!
In general, it is better to write your own code to parse input strings,
rather than use scanf.
I note that you are using memset in your code to zero your buffer
before reading input into it. The code will work equally well without
it, but I don''t know the true context this code is used in, so perhaps
it is valid.
Also, you are putting an artificial 10-character limit on the user''s
input. If the user wants to define a range between two five digit
numbers, it will not fit, and the end of the second number will be
chopped off. I usually use a function I have written to read the entire
line into dynamically allocated memory, which can be extended as
needed, but increasing the buffer size to 256 would do as well in this
case.
The function I have written to read lines from standard input can be
downloaded at http://www.nic-nac-project.de/~baseb...de/readline.c;
feel free to use it in your code.
I hope all this helps!
Jimmy Hartzell


broeisi写道:
broeisi wrote:
sscanf比scanf有什么优势?


使用时不会产生潜在的缓冲区溢出漏洞

和%s。

我有以下代码:

#include< stdio.h>
#include< string.h>

int main(void)
{
int start ,完成,价值观;
char行[10];

memset(行,0,10);


不是真的有必要......

printf("事件编号?");


使用\ n终止printf()可能会导致无输出,直到\ n发出
或执行fflush(stdout)。

fgets(line,sizeof(line),stdin);
values = sscanf(line,"%d%d",& start,& finish);

printf(" start =%d en finish =%d values =%d \ n",
start,finish,values);

返回0;
}

但如果用户输入例如:erte 5 6 ..开始输出
并且完成不会是5或6但是有些奇怪的值...

我认为sscanf会找到整数值并将这些整数值分配给变量?


不,sscanf()会尝试按照规定获得它的价值,并且会在获得它们后立即终止
,或者尽快终止它失败。在上面的

示例中,它立即失败(对于erte)和`start`并且

`fin完成'仍然没有初始化(你会更好地初始化)他们

比字符串)。你应该检查'value`是否有错误,并且你已经发现了这个。

如果不能,我怎么能做到这一点?
What advantages does sscanf offer over scanf?
It does not create potential buffer overflow vulnerability when used
with %s.
I had the following code:

#include <stdio.h>
#include <string.h>

int main(void)
{
int start, finish, values;
char line[10];

memset(line,0, 10);
Not really necessary...

printf("Event numbers ? ");
Not terminating printf() with \n may result in no output until an \n is
emitted, or fflush(stdout) is executed.
fgets(line, sizeof(line), stdin);
values = sscanf(line,"%d %d", &start, &finish);

printf("start = %d en finish = %d values = %d\n",
start, finish, values);

return 0;
}

but if tha user inputted for example : erte 5 6 ..the output of start
and finished would not be 5 or 6 but some strange value...

I thought that sscanf would find values that are integers and assign
those integer values to the variables?
No, sscanf() will try to get it''s values as specified and will
terminated as soon as it gets them, or as soon as it fails. In your
example above, it fails immediately (for "erte") and `start` and
`finish` remain uninitialised (you''d have been better initialising them
than the string). You should have checked `value` for errors, and you''d
have discovered this.
If not how can I accomplish this?




如果你想从用户

可能决定输入的任何东西中获得前两个有效整数,你必须自己处理它。 AFAIK,

那里没有C函数可以帮你。


PS

请忽略帖子通过`j ********** @ gmail.com`因为它有超过它的b / b
公平份额的不准确性。


-

BR,弗拉基米尔


避开地狱之门。使用Linux

(未知来源)



If you want to get the first two valid integers out of anything user
might decide to input, you''d have to process it yourself. AFAIK,
there''s no C function that''ll do it for you.

PS
Please ignore post by `j**********@gmail.com` as it has more than it''s
fair share of inaccuracies.

--
BR, Vladimir

Avoid the Gates of Hell. Use Linux
(Unknown source)


Re:comp.lang.c
Re: comp.lang.c
sscanf比scanf有什么优势?

我有以下代码:

#include< stdio.h>
#include< string。 h>

int main(void)
{int start,finish,values;
char line [10];

memset (第0,10行);

printf("事件编号?");

fgets(line,sizeof(line),stdin);
values = sscanf(line,"%d%d",& start,& finish);

printf(" start =%d en finish =%d values =%d \\ \\ n,
开始,结束,价值观);

返回0;
}

但如果用户输入例如:erte 5 6 ..开始
和完成的输出不会是5或6而是一些奇怪的值...

我认为sscanf会找到整数值并分配那些在teger对变量的值是什么?

如果不能如何实现这个目标?
What advantages does sscanf offer over scanf?

I had the following code:

#include <stdio.h>
#include <string.h>

int main(void)
{
int start, finish, values;
char line[10];

memset(line,0, 10);

printf("Event numbers ? ");
fgets(line, sizeof(line), stdin);
values = sscanf(line,"%d %d", &start, &finish);

printf("start = %d en finish = %d values = %d\n",
start, finish, values);

return 0;
}

but if tha user inputted for example : erte 5 6 ..the output of start
and finished would not be 5 or 6 but some strange value...

I thought that sscanf would find values that are integers and assign
those integer values to the variables?

If not how can I accomplish this?




如果系统可行,请使用getline(),甚至如果延期不是

通常被推荐。对程序施加任何限制并不是一个良好的习惯。


这就是问题所在:


values = sscanf(line,"%d%d",& start,& finish);


您的代码片段的问题在于您要分配

声明的返回值以及期望两位小数,它们之间有一个

空间:


应该工作方式应该是:


int start,finish,values;

char line [10];


memset (第0,10行);


printf(事件编号?);


fgets(行,sizeof(行) ),stdin);

sscanf(行,%d,%d,& start,& finish);

sscanf(行, "%d%d"& value);


Hopw帮助并且没有编译问题。



If possible on your system, use getline(), even if extensions are not
usually reccomended. Imposing arbitary limits on a program is not a
good habit to get into.

Here lies the problem:

values = sscanf(line,"%d %d", &start, &finish);

The problem with your slice of code is that you are assigning the
return value of the statement as well as expecting two decimals with a
space between them.:

The should-work way should be:

int start, finish, values;
char line[10];

memset(line,0, 10);

printf("Event numbers ? ");

fgets (line, sizeof(line), stdin);
sscanf (line, "%d","%d", &start, &finish);
sscanf (line, "%d %d", &value);

Hopw that helps and has no compile problems.


这篇关于程序员想问一下关于sscanf的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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