0

I am trying to parse a text file into variables for x1, y1, x2 and y2 but when I print the variables to check if they have been updated to the new values and they are all 0.000000.

Sample line from the txt file is "W 0.2 0.1 0.2 0.2" formatted as "command x1 y1 x2 y2".

void draw_walls(FILE *stream)
{
    while (!feof(stream))
    {
        char command;
        // Declare variables
        double wall_x1, wall_y1, wall_x2, wall_y2;

        int wall_count = fscanf(stream, "%c %lf %lf %lf %lf", &command, &wall_x1, &wall_y1, &wall_x2, &wall_y2);
        printf("%c %lf %lf %lf %lf", command, wall_x1, wall_y1, wall_x2, wall_y2);
    }
}

printf should return "W 0.2 0.1 0.2 0.2"

currently only returns " 0.000000 0.000000 0.000000 0.000000"

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 6
    You will want to look at [**Why is while ( !feof (file) ) always wrong?**](https://stackoverflow.com/questions/5431941/why-is-while-feoffile-always-wrong) – David C. Rankin Aug 18 '19 at 07:08
  • 1
    You should print `wall_count`. It will be 5 if the `fscanf` succeeded. – user3386109 Aug 18 '19 at 07:08
  • printf("%d", wall_count); returns 335151515151515151515151515151 – Callum McNeilage Aug 18 '19 at 07:17
  • @CallumMcNeilage Then its wrong. Does parser file contains same type of info in each line ? Also print this `printf("%d, ", wall_count);` for readbility – Achal Aug 18 '19 at 07:29
  • 1
    `fscanf(stream, " %c%lf%lf%lf%lf", ...` space needed before %c, redundant before %lf – pmg Aug 18 '19 at 07:39
  • 3, 3, 5, 1, 5, 1, 5, 1, 5, 1, 5, 1, 5, 1, 5, 1, 5, 1, 5, 1, 5, 1, 5, 1, 5, 1, 5, 1, 5. Ok so it's the length of each line which is correct. I'm really sorry if I'm being annoying, I have very little experience with C – Callum McNeilage Aug 18 '19 at 07:40
  • Please add the sample data and output to the question, not to a comment where it cannot be formatted. – Jonathan Leffler Aug 18 '19 at 08:00
  • 1
    Recommendation: read a line at a time with `fgets()` — `char buffer[4096]; while (fgets(buffer, sizeof(buffer), stream) != 0) { … }`, and then use `sscanf()` to parse the line. Note that this means you can print the whole line for diagnostic purposes — which can be utterly invaluable. **Did you read a count of the lines before calling this function (or some other value)?** If so, the newline after that number was left in the input, and the first `command` value was `'\n'`, and the conversions of the `double` values failed on the `W` (it isn't numeric). – Jonathan Leffler Aug 18 '19 at 08:04

1 Answers1

0

Did you try the following check?

if (stream== NULL)
  printf("The file cannot be read.\n");

This will ensure that you are able to read the file first. I agree with "David C. Rankin". Also try checking the passing parameters if passed correctly by reference. Secondly, check the format specifier and file. feof() returns 0 for only EOF and not for any other errors encountered. So you need to explicitly check conditions other than EOF as I mentioned. Reference : https://www.geeksforgeeks.org/eof-and-feof-in-c/

Ashwani
  • 1,938
  • 11
  • 15
  • Confirm that your file format and format specifiers match. https://www.geeksforgeeks.org/scanf-and-fscanf-in-c-simple-yet-poweful/ You may notice that to adjust the "\n" character after "CITY", he added extra space int the fcanf() format specifier. – Sidharth Singh Aug 18 '19 at 08:03