Avatar of jeffiepoo
jeffiepoo
Flag for United States of America

asked on 

sscanf in c problem

Ok, so my problem is, I am trying to use sscanf to get a string and a floating point number, however for some reason it is missing the floating point number altogether. I used BOTH fputs AND printf in the following example to illustrate what IS getting scanned in, and what is SUPPOSED to be getting scanned in. I will include the output as well. Thanks for the help guys. (This is not the whole program, this is just the guts of the function), I NEED THAT FLOATING POINT NUMBER SCANNED IN!  A list of things I have tried is at the very bottom too.

FILE *file_input;
  file_input=fopen(filename,"r");
  if (file_input == NULL){
    return 1;}
    double a;
    char string [100];
    char temp_string[30];
    while (fgets(string,sizeof(string),file_input)!=NULL){
      sscanf(string,"%s\t%f",temp_string,&a);
      fputs(string, stdout);
printf("%s\t%f\n", temp_string,&a);

OUTPUT:
sneffels> a.out example.txt
PLANET  AU
PLANET  0.000000
Mercury 0.39
Mercury 0.000000
Venus   0.72
Venus   0.000000
Earth   1
Earth   0.000000
Mars    1.52
Mars    0.000000
Jupiter 5.20
Jupiter 0.000000
Saturn  9.54
Saturn  0.000000
Uranus  19.18
Uranus  0.000000
Neptune 30.06
Neptune 0.000000
Pluto   39.44
Pluto   0.000000
PLANET  AU
PLANET  0.000000
Mercury 0.39
Mercury 0.000000
Venus   0.72
Venus   0.000000
Earth   1
Earth   0.000000
Mars    1.52
Mars    0.000000
Jupiter 5.20
Jupiter 0.000000
Saturn  9.54
Saturn  0.000000
Uranus  19.18
Uranus  0.000000
Neptune 30.06
Neptune 0.000000
Pluto   39.44
Pluto   0.000000

I've tried sscanf(string,"%s\t%f",temp_string,a);   but I get a segfault
Ive tried sscanf(string,"%s%f",temp_string,&a);   without the tab character still no luck
I've tried sscanf(string,"%s %f",temp_string,&a); with just a space and that doesn't work either.
C

Avatar of undefined
Last Comment
Infinity08

8/22/2022 - Mon