Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #76236

Re: Please help -- cannot read double data from file into array

From James Kuyper <jameskuyper@verizon.net>
Newsgroups comp.lang.c
Subject Re: Please help -- cannot read double data from file into array
Date 2015-11-17 06:51 -0500
Organization A noiseless patient Spider
Message-ID <n2f481$ckt$1@dont-email.me> (permalink)
References <bb0cef7b-950a-4fcb-a7c9-2a8fbe558cd7@googlegroups.com>

Show all headers | View raw


On 11/17/2015 03:39 AM, dakupoto@gmail.com wrote:
> 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];
> 
> 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));
> 
>   while(fgets(&data[0], 64, fPtr) != NULL)
>   {
>     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])
> {
> 
>   char *cptr = NULL;
>   double temp0 = 0;
>   double temp1 = 0;
>   unsigned int i = 0;
>   unsigned int pos = 0;
> 
>   if(cp != NULL)
>   {
>     cptr = strtok(cp, "\t");
>     while(((cptr = strtok(NULL, "\t")) != NULL) &&
>           (i < NUMROWS))
>     {
>         switch(pos)
>         {
>           case 0:
>            pos++;
>          case 1:
>            temp0 = strtod(cptr, NULL);
>            a[i][0]=temp0;
>            printf("%f\n", a[i][0]);
>            pos++;
>           break;
>           case 2 :
>            temp1 = strtod(cptr, NULL);
>            a[i][1] = temp1;
>            printf("%f\n",a[i][1]);
>            pos++;
>           break;
>           default:
>           break;
>         }
>        i++;
>       }
>     pos = 0;
>   }
> }
> 
> Please note that the print statements above 
> correctly print out the double values read
> in as:
> ,,,,,
> ,,,,,
> 0.0000049470
> 5.7103690000
> 0.0000049471
> 7.3173590000
> 0.0000049472
> 5.2198320000
> 0.0000049473
> -2.0194100000
> 0.0000049474
> -9.5860700000
> 0.0000049475
> -17.3948000000
> 0.0000049476
> -21.9167000000
> 0.0000049477
> -20.4255000000
> 0.0000049479
> -10.0376000000
> 0.0000049480
> -2.7841500000
> .....
> .....
> 
> Now the problem starts. If I try to 
> use the contents of the 2 dimensional array
> in any subsequent calculation, I find that
> each of the stored values are zero.

The only two-dimension array I could find in your program is named
indata. Since it's a file scope array with no explicit initializer, it
is implicitly zero-initialized. The code you've shown us contains not a
single statement that would change the value of any element of that
array. Therefore, it seems reasonable that the array would contain only
zeroes.
-- 
James Kuyper

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


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