sscanf问题 [英] sscanf question
问题描述
给出以下内容
#include< stdio.h>
int main(无效)
{
char line [BUFSIZ];
long arg1,arg2;
while(fgets(line,BUFSIZ,stdin) )!= NULL){
if(sscanf(line,"%ld%ld",& arg1,& arg2)== 2){
snprintf (line,sizeof(line),"%ld \ n",arg1 + arg2);
if(fputs(line,stdout)== EOF){
fprintf(stderr,输出错误\ n);
}
}
else {
fprintf(stderr,invalid input\\\
);
}
}
返回0;
}
当我运行它时,我得到以下
[cdalten @ localhost oakland] $ ./add
4 5
9
65
无效输入
现在,这里'这是个问题。怎么在
数字之间有空格?在这种情况下,sscanf()甚至没有格式为
args的空格。即,它就像以下一样
(sscanf(line,"%ld%ld",& arg1,& arg2)
更重要的是。当我进入65时,计算机不会吐出
返回11?
Given the following
#include <stdio.h>
int main(void)
{
char line[BUFSIZ];
long arg1, arg2;
while(fgets(line, BUFSIZ, stdin) != NULL){
if(sscanf(line,"%ld%ld",&arg1,&arg2) == 2){
snprintf(line, sizeof(line), "%ld\n", arg1 + arg2);
if(fputs(line, stdout) == EOF){
fprintf(stderr, "output error\n");
}
}
else{
fprintf(stderr, "invalid input\n");
}
}
return 0;
}
When I run it, I get the following
[cdalten@localhost oakland]$ ./add
4 5
9
65
invalid input
Now, here''s the question. How come there has to be a space between the
numbers? sscanf() in this case doesn''t even have a space in the format
args. Ie, it is like the following
(sscanf(line,"%ld%ld",&arg1,&arg2)
More to the point. How come when I enter 65, the computer won''t spit
back 11?
推荐答案
./ add
4 5
9
65
无效输入
现在,问题就在这里。为什么
数字之间必须有空格?sscanf()在这种情况下甚至没有空格格式
args。即,它类似于以下
(sscanf(line,"%ld%ld",& arg1,& arg2)
更重要的是。当我进入65时,计算机不会吐痰
返回11?
./add
4 5
9
65
invalid input
Now, here''s the question. How come there has to be a space between the
numbers? sscanf() in this case doesn''t even have a space in the format
args. Ie, it is like the following
(sscanf(line,"%ld%ld",&arg1,&arg2)
More to the point. How come when I enter 65, the computer won''t spit
back 11?
Chad说:
Chad said:
给出以下内容
#include< stdio.h> ;
int main(无效)
{
char line [BUFSIZ];
l ong arg1,arg2;
while(fgets(line,BUFSIZ,stdin)!= NULL){
if(sscanf(line,"%ld) %ld",& arg1,& arg2)== 2){
Given the following
#include <stdio.h>
int main(void)
{
char line[BUFSIZ];
long arg1, arg2;
while(fgets(line, BUFSIZ, stdin) != NULL){
if(sscanf(line,"%ld%ld",&arg1,&arg2) == 2){
< snip>
<snip>
当我运行它时,我得到以下
[cdalten @ localhost oakland]
When I run it, I get the following
[cdalten@localhost oakland]
./ add
4 5
9
65
无效输入
现在,这是问题所在。怎么在
数字之间有空格?
./add
4 5
9
65
invalid input
Now, here''s the question. How come there has to be a space between the
numbers?
如果没有必要,你会如何增加65到27个?
-
Richard Heathfield< http://www.cpax.org.uk>
电子邮件:-http:// www。 + rjh @
谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>
Usenet是一个奇怪的放置" - dmr 1999年7月29日
If there didn''t have to be, how would you add sixty-five to twenty-seven?
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
这篇关于sscanf问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!