1

Following the scanset format in this question, I tried a slightly different format to read several numbers instead of one. Here's my data:

722280,"BIRMINGHAM MUNICIPAL AP",AL,-6.0,33.567,-86.750,189

Here's my code:

char buf[400];
char station[11], city[101], state[11];
int tz, lat, lon, alt;
fgets(buf, sizeof buf, file) // yes, I test this 
sscanf( buf, "%10[^,],%100[^,],%10[^,],%d,%d,%d,%d", station, city, state, &tz, &lat, &lon, &alt);

When I run this, station, city, state and tz are all set properly. However, lat, lon and alt are not - lat is 1 for instance, lon is 0.

I tried a number of variations on the string, including [^,] after the %d, and removing the commas, without luck.

Yes, I know I can do this with strtok or one of the many variants, but I'd like to try a sscanf as it matches a similar set of code and I'd like to keep them similar, if possible.

I suspect this is possible and I'm doh'ing on the format?

Maury Markowitz
  • 9,082
  • 11
  • 46
  • 98
  • 1
    They are floating point, not `int`. You are using `%d` format in `sscanf`. – Weather Vane Apr 14 '18 at 21:50
  • OMG. That's what you get for a C&P. – Maury Markowitz Apr 14 '18 at 21:51
  • 1
    ALWAYS check the return value fom `scanf` family: the number of items successfully converted. – Weather Vane Apr 14 '18 at 21:52
  • I do the test in real code, this is my sim. Hmmm, so I did this: "%10[^,],%100[^,],%10[^,],%d,%f,%f,%d" and I get the same behaviour. – Maury Markowitz Apr 14 '18 at 21:54
  • 1
    You have 3 floating point values in the text input example, but only 2 `%f` in the commented "real code". And are they `int` or `float` or `double`? The variables are all `int`. – Weather Vane Apr 14 '18 at 21:56
  • I suggest `11` is not enough for a state name such as "New Hampshire". You restrict the input length, but the unread data remains in the input buffer, to be read for the next format specifier: the excess data is not discarded. – Weather Vane Apr 14 '18 at 22:02
  • when asking a question about a run time problem, as this question is doing, always post a [mcve] so we can reproduce the problem to help you debug it. – user3629249 Apr 15 '18 at 03:34

1 Answers1

1

Use the right format as suggest, as well as right data types. This should work:

int main(void)
{
    FILE *file = fopen("test.txt", "r");

    char buf[400];
    char station[11], city[101], state[11];
    float tz, lat, lon;
    int alt;

    while(fgets(buf, sizeof(buf), file))
    {
        sscanf(buf, "%10[^,],%100[^,],%10[^,],%f,%f,%f,%d", 
            station, city, state, &tz, &lat, &lon, &alt);

        printf("%s\n%s\n%s\n%f\n%f\n%f\n%d\n\n", station, city, state, tz, lat, lon, alt);
    }

    return 0;
}
Barmak Shemirani
  • 30,904
  • 6
  • 40
  • 77
  • 1
    You would need further processing on `city` to remove the quotes. Reading the city into a `tmp` variable and then using `strncpy (city, tmp +1, strlen (tmp) - 2)` or something similar would do. (unless you want useless double-quotes hanging off each end of the string) – David C. Rankin Apr 15 '18 at 03:07
  • 1
    regarding: `FILE *file = fopen("test.txt", "r"); Always check (!=NULL) the returned value to assure the operation was successful. If not successful, call `perror( "fopen test.txt for read failed" );` This will output the enclosed text AND the text reason the system thinks the function failed to `stderr`. Follow that call with `exit( EXIT_FAILURE );` – user3629249 Apr 15 '18 at 03:37