sscanf not working , why?

B

baumann@pan

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,&thirdchar,&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
 
B

baumann@pan

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

baumann@pan

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 ???
 
Z

Zoran Cutura

baumann@pan said:
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,&thirdchar,&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.
 
B

baumann@pan

Zoran said:
baumann@pan said:
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,&thirdchar,&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,&thirdchar,&f,laststr);
to
sscanf(file,"%s %i %i %c %f
%s",firststr,&i2,&i3,&thirdchar,&f,laststr);

it prints what i want. thanks
 
M

Martin Ambuhl

baumann@pan said:
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'
 
M

Martin Ambuhl

baumann@pan said:
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.
 
B

baumann@pan

Martin said:
baumann@pan said:
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,&thirdchar,&f,laststr);
printf("count:%d, %s %i %i %c %f %s\n", count, firststr, i2,i3,
thirdchar,f,laststr);

it works well.
 
B

baumann@pan

Martin said:
baumann@pan said:
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.
 
P

pete

Martin said:
^^^^
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."
 
K

Keith Thompson

pete said:
Martin said:
^^^^
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top