I have a text file, consisting of lines of floating point values delimited
by commas, which I need to read into an array within a C program.
The floating point values are of variable width (but can be made to be of
fixed width if it makes reading them in easier), for example:
0.123,0.1,0.45422,1.433,5.9,0.22234 (carriage return)
0.12,4.356,1.235,6.3,1.65542,0.002453
.
.
.
(etc.)
There will always be the same number of values per line.
How, in C, do I read these into an array? Every file reading function I've
come across seems to require a fixed length, or only stops reading at a
carriage return.
Your help would be much appreciated
Thanks
El.
--
comp.lang.c.moderated - cl...@plethora.net
>I have a text file, consisting of lines of floating point values delimited
>by commas, which I need to read into an array within a C program.
>
>The floating point values are of variable width (but can be made to be of
>fixed width if it makes reading them in easier), for example:
>
>There will always be the same number of values per line.
I would suggest reading each line into a string buffer then parsing the
string buffer. By creating a large enough buffer, you should be able to
read in the entire line and then simply pull out the individual numbers.
Here is some code that should accomplish this for you. Note: this code is
untested and being typed online.
char buf[128]; /* largish buffer */
fgets(buf, sizeof(buf), input_stream);
This fgets() should read in your input line (assuming it is less than 128
bytes long, if it is longer, make buf bigger) so you can parse out the
numbers.
If you are concerned about the size of buf, make it some reasonably large
number such as 1024 or 2048.
--
comp.lang.c.moderated - cl...@plethora.net
For something like this, fscanf should work reasonably well:
fscanf("%f,", &some_float);
will read one float at a time from your file. On the last item on the
line, it'll fail but should (IIRC) have already read the value before
failing.
You might prefer to use a format for an entire line at a time:
fscanf("%f,%f,%f,%f,%f,%f", f, f+1, f+2, f+3, f+4, f+5);
The new-line at the end of the line will automatically be skipped
prior to the first numeric conversion in the next call to fscanf, so
you can pretty much ignore it; %f and such automatically skip any
whitespace prior to a character suitable for conversion, and a new-
line is considered whitespace.
The only problem you'll have arises if you can possibly have a value
that's been skipped in the file, signified by two commas (or a comma
at the end of the line) with no value provided. Since it sounds like
you can change the format of the file as needed, it's probably easiest
to simply ensure that this never arises.
--
comp.lang.c.moderated - cl...@plethora.net
> I have a text file, consisting of lines of floating point values delimited
> by commas, which I need to read into an array within a C program.
>
> The floating point values are of variable width (but can be made to be of
> fixed width if it makes reading them in easier)
How about this approach:
int read_doubles(FILE* in, double* array) {
int i, c;
/* loop while double floats can be read */
for (i= 0; fscanf(in, "%lf", array+i) == 1; i++)
/* if fscanf bumped into something _not_
equal to a comma, put it back, e.g.
a new line followed a double float number
*/
if ((c= fgetc(in)) != ',')
ungetc(c, in);
/* return total numbers read */
return i;
}
An alternative would be to read an entire line (if you know that maximum
line size) and use strtok() repeatedly to separate the commas from the
numbers,
but I prefer the first approach.
kind regards,
Jos aka j...@and.nl
--
comp.lang.c.moderated - cl...@plethora.net