fscanf(...) not completing the read
Edit, Just to put this at the top: I have followed the advice here and changed strategy to use strtok() and strtod().
The goal is to read a CSV file and put the values into variables. I have extracted the misbehaving code to a test program and it manifests the problem. The printf at line 79 looks good, but the next printf at line 79 shows only one character read. I am expecting a lot more. The same concepts applies within the for loop. Conclusion: There is something about the fscanf(…) that I do not understand. Here is the code. Edit: I simplified the code, shortened the input file to remove non-symptomatic lines, and am providing the output. In summary, the fscanf(...) is reading one field at a time, one one line. I checked errno immediately after the fscanf call and it is always zero. After reading the post from GazL, I was certain that would resolve the problem. Alas, there is another defect that I am looking right at but cannot see. Thank you for your time and patience. Code:
// CentOS Linux release 7.7.1908 (Core) Code:
0, 0.0001, 0.0002, 0.0003, 0.0004, 0.0005, 0.0006, 0.0007 Code:
[mcs@localhost sim]$ ./a.out |
DEBUG IT
You started to debug field_count in that compiled out code. Debug it for the problem lines. Perhaps the sample of data shown should show, lines before, on, and after line 79. Use GDB and break at the fscanf() call, step over it and check the outcome. Check ERRNO if field_count is negative. Move the problem lines up to start at line 2 and see what happens. Use fgets() and see what it thinks about those lines. Read the whole file into a RAM buffer and examine that in the debugger. |
scanf() doesn't treat a comma as a field-separator. When your input contains commas you need to specify them in the format string something like scanf(" %d, %f, %f, ...", &args, ...) else you'll get a format mismatch when it hits the comma.
Oh, and use 'free()' not 'delete' in a C program. |
Figured I'd give that a small try, but your code has numerous compile errors.
|
Quote:
|
Quote:
Thanks for giving it a try. |
In addition to the delimiter problem that GazL pointed out, the code has a buffer overrun on the string variables. The safest method is to use getline to read the line and allocate a buffer, and then parse the line yourself.
Ed |
Quote:
foughtonp: Yes, there is probably such a library. But, once past the header, every line of data takes the exact same format. In the meantime, I will be looking a sscanf(), or scans(), or something like that to read into a string the parse the string. Extra code but maybe I can make that work. Thank you for your time and patience. |
Probably better to skip using a fully inclusive library function like any of the scanf() variety and processing line by line in character arrays, looking for the comma delimiter.
More exhaustive code, but that's what loops are for. Hey, ... done plenty decoding serial protocols, especially since you don't get the entire packet all the time. At least here you do not have to have a state machine which is re-entrant to remember your last parsed point, you just validate line by line from a file. |
%s will eat the comma in the input, so there won't be one to match the literal comma in the scanf format string. It should work with %d and %f however.
Here's a minimal example to show it works with your input file: Code:
#include <stdio.h> scanf() is notoriously poor at dealing with malformed input. If robustness matters you'll likely be better off using getline() with strtok() and strtod(). |
Ok, I have changed my strategy and am going with the strtok and the strtod.
Wrote some test code and got it working. Thank you to each of you for your time and patience. |
All times are GMT -5. The time now is 03:50 PM. |