0

I'm trying to read the numbers after a colon and store the value in a variable, but when I print it, it prints a random 6 digit number. I only need to store the value, not the 'ms' or 'degrees'.

For example, the text file is similar to this, but repeats for 100 set of values:

time: 20 ms
temperature: 50.5 degrees
lightvalue: 30
value1: 0.59
value2: 1
value3: 0
----------------------

time: 40 ms
temperature: 37 degrees
lightvalue: 10
value1: 1.57
value2: 0
value3: 1
----------------------

I want to store each number in a separate variable.

Here is part of my code:

int time[10];
double temperature[10];
int lightvalue[10];
double value1[10];
int value2[10];
int value3[10];


for (int i = 0; i < 100; i++) {
    fscanf(infile, "time: %i", &time);
    fscanf(infile, "temperature: %d", &temperature);
    fscanf(infile, "lightvalue: %i", &light);
    fscanf(infile, "value1: %d", &val1);
    fscanf(infile, "value2: %i", &val2);
    fscanf(infile, "value3: %i", &val3);
    //how to skip the "---------------" line?
}
Furkan Yavuz
  • 1,858
  • 5
  • 30
  • 51
Bob Xie
  • 11
  • 4
  • 4
    Nothing is reading the `"ms"`. `fscanf(infile, "time: %i", &time);` --> `if (fscanf(infile, " time: %d ms", &time[i]) == 1) Success();` or better yet, 1st use `fgets()`. – chux - Reinstate Monica Dec 13 '17 at 02:27
  • 6
    Tip: when code is not reading input as expected, insure code checks the return value from `fscanf()`. – chux - Reinstate Monica Dec 13 '17 at 02:30
  • 1
    You cause undefined behaviour by using the wrong format specifier and scanf argument (on every single line) – M.M Dec 13 '17 at 03:05
  • 2
    Read the lines with `fgets()`; scan the lines with `sscanf()`. Get the types right (temperature is a `double`; use `%lf` to read that value). Use the arrays properly: `&time[i]`, `&temperature[i]`, etc. Spell things correctly — `value1` defined; `val1` referenced. Skip dash lines by reading and ignoring lines that start with a dash. Skip blank lines by reading and ignoring lines that only contain blanks (or nothing). Watch out for `fgets()` including the newline — that mostly won't matter. Check the result of each `sscanf()`. Don't loop from 0 to 100 when the arrays are size 10. Etc. – Jonathan Leffler Dec 13 '17 at 03:19
  • 2
    Have you learned about structures yet? I assume not. If you have, this cries out for the use of a structure type (or an array of a structure type). – Jonathan Leffler Dec 13 '17 at 03:25

1 Answers1

0

Your code contains several errors. You have used different variable names in iteration. You used wrong specifiers.

for (int i = 0; i < 2; i++) {
    fscanf(infile, "time: %d %*s\n", &time[i]);
    fscanf(infile, "temperature: %lf %*s\n", &temperature[i]);
    fscanf(infile, "lightvalue: %i\n", &light[i]);
    fscanf(infile, "value1: %lf\n", &val1[i]);
    fscanf(infile, "value2: %i\n", &val2[i]);
    fscanf(infile, "value3: %i\n", &val3[i]);
    fscanf(infile, "%*s\n"); // to skip the "---------------" line
}

To skip rest of the line, you may use %*s which will match the unused string.

abdullah
  • 662
  • 5
  • 7
  • 1
    Note [Trailing white space in `scanf()` format strings](https://stackoverflow.com/questions/19499060/). You'd do better with a leading blank on the format strings; that would skip white space, including newlines. – Jonathan Leffler Dec 13 '17 at 03:22