We are printing Linux device events in one program than trying to read and interpret the data by piping the input of that program into another.
First program's relevant code:
struct input_event event1;
...
// open correct event file and read its values into the input event struct
...
printf("%d %zu.%-6zu %d %3d %9d\n", 1, event1.time.tv_sec, event1.time.tv_usec, event1.type, event1.code, event1.value);`
Output of first program in the terminal, looks correct.
1 1.638837 3 16 -24402
1 1.638837 3 17 23791
1 1.646835 3 16 -25588
1 1.646835 3 17 23753
1 1.654835 3 16 -26378
1 1.654835 3 17 21386
1 1.666836 3 16 -27498
1 1.666836 3 17 20308
1 1.674837 3 16 -29342
1 1.674837 3 17 18248
1 1.682836 3 16 0
Programs piped together in bash: ./prog1 | ./prog2
Second program's relevant code:
int event_num, type, code, value;
double time;
char arr[100];
// this scanf returns 0 on this and following printf is garbage
scanf("%d %lf %d %d %d", &event_num, &time, &type, &code, &value);
// alternately, this scanf returns 5 as expected and printf works,
// but my first value is stored in a character array
// rather than an integer variable as wanted
scanf("%s %lf %d %d %d", arr, &time, &type, &code, &value);
It seems as though I can read the stdin as an array of strings no problem and I can store most of the input as expected, but I cannot seem to get the first value to be stored as an integer.
Thanks in advance, and let me know if you have any further questions or clarification needed.
I tried different format specifiers including %*s and the %[^0-9]%d and getchar() hoping to remove any junk at the beginning of the string that might be messing stuff up. I have tried changing the type of the variable and the format specifier, but I can never read the first value as anything other than a string.
fgetsto read the whole line and the print the string to see what the real input is.. Perhaps print the decimal value of each character to find "non-printable" characters:-)