By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,169 Members | 1,705 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,169 IT Pros & Developers. It's quick & easy.

sscanf not working , why?

P: n/a
hi,
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

Nov 14 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
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.

Nov 14 '05 #2

P: n/a
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 ???

Nov 14 '05 #3

P: n/a
baumann@pan <ba*********@gmail.com> wrote:
hi,
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


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
Nov 14 '05 #4

P: n/a


Zoran Cutura wrote:
baumann@pan <ba*********@gmail.com> wrote:
hi,
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.
thanks, i take it for grant, indeed it's not true,ie, characters are
not consumed.

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

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).


indeed, when I delimit the each format string with space, the 2nd
program works well, why?
ie.
change
sscanf(file,"%s%i%i%c%f%s",firststr,&i2,&i3,&third char,&f,laststr);
to
sscanf(file,"%s %i %i %c %f
%s",firststr,&i2,&i3,&thirdchar,&f,laststr);

it prints what i want. thanks

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


Nov 14 '05 #5

P: n/a
baumann@pan wrote:
hi,
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?
Each sscanf() has as its first argument 'file' which has a constant address.

[...] how can i get the right result? thanks


#include <stdio.h>

int main(void)
{
char *input_string = "aaa 23 32 m 2.23 ammasd";
int first_int, second_int;
int chars_seen = 0, field;

char initial_string[23];
char character_after_ints;
sscanf(input_string, "%s%n", initial_string, &field);
chars_seen += field;
printf
("initial string: \"%s\", last field size: %d, chars consumed:
%d\n",
initial_string, field, chars_seen);

sscanf(input_string + chars_seen, "%d%n", &first_int, &field);
chars_seen += field;
printf("first int: %d, last field size: %d, chars consumed: %d\n",
first_int, field, chars_seen);

sscanf(input_string + chars_seen, "%d%n", &second_int, &field);
chars_seen += field;
printf("second int: %d, last field size: %d, chars consumed: %d\n",
second_int, field, chars_seen);

sscanf(input_string + chars_seen, "%c", &character_after_ints);
printf("(space after second int) '%c'\n", character_after_ints);
sscanf(input_string + chars_seen + 1, "%c", &character_after_ints);
printf("(char after space) '%c'\n", character_after_ints);
return 0;
}

Gives the following output. I'm sure you can figure it out.

initial string: "aaa", last field size: 3, chars consumed: 3
first int: 23, last field size: 3, chars consumed: 6
second int: 32, last field size: 3, chars consumed: 9
(space after second int) ' '
(char after space) 'm'
Nov 14 '05 #6

P: n/a
baumann@pan wrote:
also the msdn library vs.net 2003 has the example

#include <stdio.h>
void main( void )

^^^^
Anything giving illegal definitions of main should not be used.
Nov 14 '05 #7

P: n/a


Martin Ambuhl wrote:
baumann@pan wrote:
hi,
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?
Each sscanf() has as its first argument 'file' which has a constant address.

[...]
how can i get the right result? thanks


#include <stdio.h>

int main(void)
{
char *input_string = "aaa 23 32 m 2.23 ammasd";
int first_int, second_int;
int chars_seen = 0, field;

char initial_string[23];
char character_after_ints;
sscanf(input_string, "%s%n", initial_string, &field);
chars_seen += field;
printf
("initial string: \"%s\", last field size: %d, chars consumed:
%d\n",
initial_string, field, chars_seen);

sscanf(input_string + chars_seen, "%d%n", &first_int, &field);
chars_seen += field;
printf("first int: %d, last field size: %d, chars consumed: %d\n",
first_int, field, chars_seen);

sscanf(input_string + chars_seen, "%d%n", &second_int, &field);
chars_seen += field;
printf("second int: %d, last field size: %d, chars consumed: %d\n",
second_int, field, chars_seen);

sscanf(input_string + chars_seen, "%c", &character_after_ints);


why sscanf treat the delimit sign ' ' as an input when it encounter
format %c?
and read it and store it in the user provided variable?it's unusal.

do you read the 2nd post ??

in which i change the format string as

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);

it works well.

printf("(space after second int) '%c'\n", character_after_ints);
sscanf(input_string + chars_seen + 1, "%c", &character_after_ints);
printf("(char after space) '%c'\n", character_after_ints);
return 0;
}

Gives the following output. I'm sure you can figure it out.

initial string: "aaa", last field size: 3, chars consumed: 3
first int: 23, last field size: 3, chars consumed: 6
second int: 32, last field size: 3, chars consumed: 9
(space after second int) ' '
(char after space) 'm'


Nov 14 '05 #8

P: n/a


Martin Ambuhl wrote:
baumann@pan wrote:
hi,
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?
Each sscanf() has as its first argument 'file' which has a constant address.

[...]
how can i get the right result? thanks


#include <stdio.h>

int main(void)
{
char *input_string = "aaa 23 32 m 2.23 ammasd";
int first_int, second_int;
int chars_seen = 0, field;

char initial_string[23];
char character_after_ints;
sscanf(input_string, "%s%n", initial_string, &field);
chars_seen += field;
printf
("initial string: \"%s\", last field size: %d, chars consumed:
%d\n",
initial_string, field, chars_seen);

sscanf(input_string + chars_seen, "%d%n", &first_int, &field);
chars_seen += field;
printf("first int: %d, last field size: %d, chars consumed: %d\n",
first_int, field, chars_seen);

sscanf(input_string + chars_seen, "%d%n", &second_int, &field);
chars_seen += field;
printf("second int: %d, last field size: %d, chars consumed: %d\n",
second_int, field, chars_seen);

sscanf(input_string + chars_seen, "%c", &character_after_ints);


why sscanf treat the delimit sign ' ' as an input when it encounter
format %c?
and read it and store it in the user provided variable?it's unusal.

do you read the 2nd post ??
in which i change the format string as

count =
sscanf(file,"%s %i %i %c %f
%s",fir*ststr,&i2,&i3,&thirdchar,&f,la*ststr);
printf("count:%d, %s %i %i %c %f %s\n", count, firststr, i2,i3,
thirdchar,f,laststr
);

it works well.
printf("(space after second int) '%c'\n", character_after_ints);
sscanf(input_string + chars_seen + 1, "%c", &character_after_ints);
printf("(char after space) '%c'\n", character_after_ints);
return 0;
}

Gives the following output. I'm sure you can figure it out.

initial string: "aaa", last field size: 3, chars consumed: 3
first int: 23, last field size: 3, chars consumed: 6
second int: 32, last field size: 3, chars consumed: 9
(space after second int) ' '
(char after space) 'm'


Nov 14 '05 #9

P: n/a
Martin Ambuhl wrote:

baumann@pan wrote:
also the msdn library vs.net 2003 has the example

#include <stdio.h>
void main( void )

^^^^
Anything giving illegal definitions of main should not be used.


I don't think that's illegal.
I think it falls under the clause

N869
5.1.2.2.1 Program startup
[#1]
"or in some other implementation-defined manner."

--
pete
Nov 14 '05 #10

P: n/a
pete <pf*****@mindspring.com> writes:
Martin Ambuhl wrote:

baumann@pan wrote:
> also the msdn library vs.net 2003 has the example
>
> #include <stdio.h>
> void main( void )

^^^^
Anything giving illegal definitions of main should not be used.


I don't think that's illegal.
I think it falls under the clause

N869
5.1.2.2.1 Program startup
[#1]
"or in some other implementation-defined manner."


Which makes it legal only if the implementation defines it to be
legal, i.e., if the implementation *explicitly* documents it supports
"void main(void)". I don't know whether whatever compiler the library
is used with actually documents this (I sincerely hope it doesn't).

Of course, even if it's legal, that doesn't imply that it's not evil.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.