sscanf不工作,为什么? [英] sscanf not working , why?

查看:87
本文介绍了sscanf不工作,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



1)首先测试程序代码


#include< stdio.h>


int main(无效)

{

char * file =" aaa 23 32 m 2.23 ammasd" ;;

int i2,i3;


浮动f;

char firststr [23];

char thirdchar;

char laststr [23];

sscanf(文件,"%s",firststr);

printf("%s \ n",firststr);

sscanf(文件,%i,& i2);

printf("%i \ n",i2);


sscanf(文件,%i,& i3);

printf("%i \ n",i3);


sscanf(文件,%c,& thirdchar);

printf("%c\ n",thirdchar);

返回0 ;

}

编程没有打印23 32,它会发出结果


aaa

-1073748344

1073792608

a

为什么?




2)第二次测试程序m代码


#include< stdio.h>


int main(无效)

{

char * file =" aaa 23 32 m 2.23 ammasd" ;;

int i2,i3;


float f;

char firststr [23];

char thirdchar;

char laststr [23];

int count;

count;

/ * sscanf(file,"%s",firststr);

printf("%s \ n" ,firststr);

sscanf(文件,%i,& i2);

printf("%i \ n",i2);


sscanf(文件,%i,& i3);

printf("%i \ n",i3);


sscanf(文件,"%c",& thirdchar);

printf("%c\ n",thirdchar); * / < br $>
count =

sscanf(文件,%s%i%i%c%f%s,firststr,& i2,& i3,& third char,& f,laststr);

printf(" count:%d,%s%i%i%c%f%s \ n",count,firststr,i2,i3 ,

thirdchar,f,laststr);

返回0;

}


给了我结果


数:4,aaa 23 32 36.759842 ??°


我怎样才能得到正确的结果?谢谢


baumann @ pan


1) first test program code

#include <stdio.h>

int main(void)
{
char * file = "aaa 23 32 m 2.23 ammasd";
int i2,i3;

float f;
char firststr[23];
char thirdchar;
char laststr[23];
sscanf(file,"%s",firststr);
printf("%s\n" ,firststr);
sscanf(file,"%i",&i2);
printf("%i\n",i2);

sscanf(file,"%i",&i3);
printf("%i\n",i3);

sscanf(file,"%c", &thirdchar);
printf("%c\n",thirdchar);
return 0;
}
the prog doesn''t print 23 32 ,it emits the result

aaa
-1073748344
1073792608
a

why?

while
2) second test program code

#include <stdio.h>

int main(void)
{
char * file = "aaa 23 32 m 2.23 ammasd";
int i2,i3;

float f;
char firststr[23];
char thirdchar;
char laststr[23];
int count;
count;
/*sscanf(file,"%s",firststr);
printf("%s\n" ,firststr);
sscanf(file,"%i",&i2);
printf("%i\n",i2);

sscanf(file,"%i",&i3);
printf("%i\n",i3);

sscanf(file,"%c", &thirdchar);
printf("%c\n",thirdchar);*/
count =
sscanf(file,"%s%i%i%c%f%s",firststr,&i2,&i3,&third char,&f,laststr);
printf("count:%d, %s %i %i %c %f %s\n", count, firststr, i2,i3,
thirdchar,f,laststr);
return 0;
}

gives me the result

count:4, aaa 23 32 36.759842 ??°

how can i get the right result? thanks

baumann@pan

推荐答案

msdn库vs.net 2003也有示例


#include< stdio.h>

void main(无效)

{

char tokenstring [] =" 15 12 14 ..." ;;

char s [81];

char c;

int i;

float fp;


/ *从tokenstring输入各种数据:* /

sscanf(tokenstring,& ;%s",s);

sscanf(tokenstring,"%c",& c);

sscanf(tokenstring,"%d"& amp; ; i);

sscanf(tokenstring,"%f"& fp);


/ *输出数据读取* /

printf(" String =%s \ n",s);

printf(" Character =%c\ n",c);

printf(" Integer:=%d \ n",i);

printf(" Real:=%f \ n",fp);

}

输出


S. tring = 15

Character = 1

整数:= 15

Real:= 15.000000


i我不知道上面给出的第一个例子有什么区别



also the msdn library vs.net 2003 has the example

#include <stdio.h>
void main( void )
{
char tokenstring[] = "15 12 14...";
char s[81];
char c;
int i;
float fp;

/* Input various data from tokenstring: */
sscanf( tokenstring, "%s", s );
sscanf( tokenstring, "%c", &c );
sscanf( tokenstring, "%d", &i );
sscanf( tokenstring, "%f", &fp );

/* Output the data read */
printf( "String = %s\n", s );
printf( "Character = %c\n", c );
printf( "Integer: = %d\n", i );
printf( "Real: = %f\n", fp );
}
Output

String = 15
Character = 1
Integer: = 15
Real: = 15.000000

i don''t know what''s the difference between the first example i give
above.


3)测试程序3

#include< stdio.h>


int main(无效)

{

char * file =" aaa 23 32 m 2.23 ammasd" ;;

int i2,i3;


float f;

char firststr [23];

char thirdchar;

char laststr [23];

int count;

count;

/ * sscanf(file,"%s",firststr);

printf("%s \ n" ,firststr);

sscanf(文件,%i,& i2);

printf("%i \ n",i2);


sscanf(文件,%i,& i3);

printf("%i \ n",i3);


sscanf(文件,"%c",& thirdchar);

printf("%c\ n",thirdchar); * /

count = sscanf(文件,%s%i%i%c%f

%s",firststr,& i2,& i3,& thirdchar ,& f,laststr);

printf(" count:%d,%s%i%i%c%f%s \ n",count,firststr,i2,i3,

thirdchar,f,laststr);

返回0;

}


这给我正确的结果,


数:6,aaa 23 32 m 2.230000 ammasd


但第二个例子的唯一区别是两者之间的空间

格式字符串,

为什么???

3) test program 3

#include <stdio.h>

int main(void)
{
char * file = "aaa 23 32 m 2.23 ammasd";
int i2,i3;

float f;
char firststr[23];
char thirdchar;
char laststr[23];
int count;
count;
/*sscanf(file,"%s",firststr);
printf("%s\n" ,firststr);
sscanf(file,"%i",&i2);
printf("%i\n",i2);

sscanf(file,"%i",&i3);
printf("%i\n",i3);

sscanf(file,"%c", &thirdchar);
printf("%c\n",thirdchar);*/
count = sscanf(file,"%s %i %i %c %f
%s",firststr,&i2,&i3,&thirdchar,&f,laststr);
printf("count:%d, %s %i %i %c %f %s\n", count, firststr, i2,i3,
thirdchar,f,laststr);
return 0;
}

this give me the right result,

count:6, aaa 23 32 m 2.230000 ammasd

but the only difference between the 2nd example is the space between
the format string,

why ???


baumann @ pan < BA ********* @ gmail.com>写道:
baumann@pan <ba*********@gmail.com> wrote:

1)首先测试程序代码

int main(void )
{
char * file =" aaa 23 32 m 2.23 ammasd" ;;
int i2,i3;

float f;
char firststr [23];
char thirdchar;
char laststr [23];
sscanf(file,"%s",firststr);
printf("%s \ n",firststr);
sscanf(文件,%i,& i2);


这里你给sscanf提供与之前相同的参数,它是字符串

指向的文件。现在sscanf会发现''a''成为第一个角色。

这不是%i转换可以消耗的角色。所以sscanf

停止转换返回。

如果你检查sscanf的返回值,你会看到

它是0 。


因此变量i2和i3保持未初始化并且可能保持任何值。


sscanf与fscanf相比,scanf不消耗字符串

。输入字符串保持不变,因为它被传递给sscanf



printf("%i \ n",i2);

sscanf(文件,"%i",& i3);
printf("%i \ n",i3);

sscanf(文件,"% c",& thirdchar);
printf("%c\ n",thirdchar);
返回0;
}
编程无法打印23 32,它会发出结果

aaa
-1073748344
1073792608
一个

为什么?


2)第二个测试程序代码

< stdio.h>

int main(无效)
{*> char * file =" aaa 23 32 m 2.23 ammasd" ;;
int i2,i3;

float f;
char firststr [23];
char thirdchar;
char laststr [23];
int count;
count;
/ * sscanf(file,"%s",firststr);
printf("% s \ n",firststr);
sscanf(文件,%i,& i2);
printf("%i \ n",i2);

sscanf(档案, %i,& i3);
printf(%i \ n,i3);

sscanf(文件,%c,& thirdchar );
printf("%c\ n",thirdchar); * /
count =
sscanf(文件,"%s%i%i%c%f%s" ;,firststr,& i2,& i3,& third char,& f,laststr);
printf(" count:%d,%s%i%i%c%f%s \\ \\ n,计数,firststr,i2,i3,
thirdchar,f,laststr);
返回0;
}
给我结果

数:4,aaa 23 32 36.759842?<​​br />
我怎样才能得到正确的结果?谢谢

baumann @ pan

1) first test program code

#include <stdio.h>

int main(void)
{
char * file = "aaa 23 32 m 2.23 ammasd";
int i2,i3;

float f;
char firststr[23];
char thirdchar;
char laststr[23];
sscanf(file,"%s",firststr);
printf("%s\n" ,firststr);
sscanf(file,"%i",&i2);
here you give sscanf the same argument as before, it is the string
pointed to be file. Now sscanf will find ''a'' to be the first character.
This is not a character that can be consumed by %i conversion. So sscanf
stops the conversion an returns.
If you check the return value of sscanf at this point you will see that
it is 0.

So the variables i2 an i3 remain uninitialized and may hold any value.

sscanf in contrast to fscanf and scanf does not consume the characters
of the string. The input string remains the same as befor it is passed
to sscanf.
printf("%i\n",i2);

sscanf(file,"%i",&i3);
printf("%i\n",i3);

sscanf(file,"%c", &thirdchar);
printf("%c\n",thirdchar);
return 0;
}
the prog doesn''t print 23 32 ,it emits the result

aaa
-1073748344
1073792608
a

why?

while
2) second test program code

#include <stdio.h>

int main(void)
{
char * file = "aaa 23 32 m 2.23 ammasd";
int i2,i3;

float f;
char firststr[23];
char thirdchar;
char laststr[23];
int count;
count;
/*sscanf(file,"%s",firststr);
printf("%s\n" ,firststr);
sscanf(file,"%i",&i2);
printf("%i\n",i2);

sscanf(file,"%i",&i3);
printf("%i\n",i3);

sscanf(file,"%c", &thirdchar);
printf("%c\n",thirdchar);*/
count =
sscanf(file,"%s%i%i%c%f%s",firststr,&i2,&i3,&third char,&f,laststr);
printf("count:%d, %s %i %i %c %f %s\n", count, firststr, i2,i3,
thirdchar,f,laststr);
return 0;
}

gives me the result

count:4, aaa 23 32 36.759842 ?

how can i get the right result? thanks

baumann@pan




你真的需要仔细阅读sscanf函数的文档

。这个函数是标准C库中最难理解和使用的函数之一。


注意%c只读取一个字符而不是字符串另请注意

%s的转换如何在空格处停止(实际上是字符

to)。取决于你实际想要从输入字符串中读取的内容

我可以给你一些更多的建议,但我现在不能从你的

exaple程序中认识到这一点。

-

Z(zo**********@web.de)

LISP值得学习深刻的启蒙经验

当你终于得到它时你将拥有它;这段经历会让你在剩下的日子里成为更好的程序员。 - Eric S. Raymond



You really need to read the documentation of the sscanf function
carefully. This function is one of the hardest to understand and use in
the standard C library.

Note that %c only reads exactly one character not a string and also note
how a %s conversion stops at white space (which actually are characters
to). Depending on what you actually want to read from the input string
I could give you some more advice, but I don''t recognize this from your
exaple programs for now.
--
Z (zo**********@web.de)
"LISP is worth learning for the profound enlightenment experience
you will have when you finally get it; that experience will make you
a better programmer for the rest of your days." -- Eric S. Raymond


这篇关于sscanf不工作,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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