Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Please help -- cannot read double data from file into array |
| Date | 2015-11-17 11:02 +0000 |
| Organization | A noiseless patient Spider |
| Message-ID | <874mgk28jo.fsf@bsb.me.uk> (permalink) |
| References | <bb0cef7b-950a-4fcb-a7c9-2a8fbe558cd7@googlegroups.com> |
dakupoto@gmail.com writes:
> Could some C kanguage guru please help ?
> I am trying to read double data from a
> simple text file into an array. The data
> can be read in correctly, but I am unable
> use the values read into the array, later.
>
> The input file looks like:
> 0 5.091259e-09 -2.16165e+01
> 1 5.213820e-09 -2.76803e+01
> 2 5.357648e-09 -2.74552e+01
> 3 5.524594e-09 -1.84046e+01
> 4 5.611980e-09 -1.15479e+01
> 5 5.693378e-09 -5.40278e+00
> 6 5.768690e-09 -1.01834e+00
> 7 5.847084e-09 1.373752e+00
> 8 5.910280e-09 1.400719e+00
> 9 5.978795e-09 -5.31898e-01
> 10 6.089210e-09 -7.05779e+00
> 11 6.204666e-09 -1.59755e+01
> 12 6.322522e-09 -2.39013e+01
> 13 6.456074e-09 -2.76082e+01
> 14 6.600659e-09 -2.36523e+01
> 15 6.719486e-09 -1.56844e+01
> 16 6.804431e-09 -8.88031e+00
> 17 6.883560e-09 -3.05696e+00
> 18 6.959700e-09 1.044230e+00
> 19 7.037793e-09 2.966563e+00
> ....
> ....
>
> Each double value in a line is separated
> by a tab(\t). The data is read in, line
> line as a character string, and then each
> string is parsed to red in the numerical
> values. The data is stored in a 2 dimensional
> array:
>
> const unsigned int NUMROWS = 262144;
> const unsigned int PWROF2 = 18;
> char data[64];
> FILE *fPtr = NULL;
> double indata[262144][2];
In C you need to use #define or an enum constant for this sort of
number. I imagine this code comes from C++ whose rule allow using const
ints for array sizes at file scope. That's probably why you've ended up
repeating the number.
I'd limit the number of file-scope objects (globals in the vernacular)
to the very minimum. Certainly the line buffer and the FILE pointer
should not really be here.
>
> To open and read the file I use:
> void readFile(FILE *fPtr, double a[][2])
> {
> assert(fPtr != NULL);
> assert(a != NULL);
>
> bzero(&data[0], 64*sizeof(char));
bzero is a non-standard function. Nothing wrong with that, but since C
has memset which does the same job, there is really no need to use
bzero.
sizeof (char) is 1 by definition, but you should probably just use the
line buffer size directly:
memset(data, 0, sizeof data);
Finally, there's no need to zero the line buffer anyway!
> while(fgets(&data[0], 64, fPtr) != NULL)
Again, I'd use the array size directly:
while(fgets(data, sizeof data, fPtr) != NULL)
and I prefer data to &data[0] but that's very much a style detail.
> {
> parsestring(&data[0], a);
> bzero(&data[0], 64*sizeof(char));
> }
>
> }
>
> To parse each character string read in I use:
> void parsestring(char *cp, double a[][2])
> {
I find this function is too complex. You could use sscanf instead like
this:
int row;
double d1, d2;
if (sscanf(cp, "%d %lf %lf", &row, &d1, &d2) == 3) {
a[row][0] = d1;
a[row][1] = d2;
}
You might add further checks such as ensuring that row is in range.
This raises the question of what to do when there is some error in the
data. I suggest that parsestring return an int (a bool for modern C) to
indicate that all is well. You can then write
int n_pairs = 0;
while(fgets(data, sizeof data, fPtr) != NULL &&
parsestring(data, a))
n_pairs += 1;
if (!feof(fPtr))
fprintf(stderr, "Error reading data near line %d.\n", n_pairs);
<snip>
--
Ben.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Please help -- cannot read double data from file into array dakupoto@gmail.com - 2015-11-17 00:39 -0800
Re: Please help -- cannot read double data from file into array Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-11-17 00:54 -0800
Re: Please help -- cannot read double data from file into array gamo <gamo@telecable.es> - 2015-11-17 11:36 +0100
Re: Please help -- cannot read double data from file into array Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-11-17 11:04 +0000
Re: Please help -- cannot read double data from file into array Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-11-17 11:02 +0000
Re: Please help -- cannot read double data from file into array James Kuyper <jameskuyper@verizon.net> - 2015-11-17 06:51 -0500
Re: Please help -- cannot read double data from file into array BartC <bc@freeuk.com> - 2015-11-17 12:08 +0000
Re: Please help -- cannot read double data from file into array Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-11-17 13:28 +0000
Re: Please help -- cannot read double data from file into array BartC <bc@freeuk.com> - 2015-11-17 14:20 +0000
Re: Please help -- cannot read double data from file into array dakupoto@gmail.com - 2015-11-17 20:50 -0800
Re: Please help -- cannot read double data from file into array Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-11-18 11:22 +0000
Re: Please help -- cannot read double data from file into array BartC <bc@freeuk.com> - 2015-11-18 12:15 +0000
Re: Please help -- cannot read double data from file into array dakupoto@gmail.com - 2015-11-18 23:07 -0800
Re: Please help -- cannot read double data from file into array Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-11-19 10:41 +0000
Re: Please help -- cannot read double data from file into array BartC <bc@freeuk.com> - 2015-11-19 11:13 +0000
csiph-web