• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 285
  • Last Modified:

Reading in from a file to a double array

Hi,

I need to be able to read from a file in the format eg.

0
-0.2334
0.48872
0.11330
-0.33340
-0.90202
-0.11133
0.89750
-0.10202
...

And I want it to be stored in a double array.  Any ideas how I can do this?
0
DanJW
Asked:
DanJW
1 Solution
 
sunnycoderCommented:
fscanf () is the shortest and most straight forward way

I, however, would recommend using fgets() to read in data as a string and then use a function like strtod to convert it into double. this provides you more flexibility and reliability. It is easy to perform input data validation using this technique.
0
 
sunnycoderCommented:
This is the easy way out ..

FILE * fptr;
int i=0;
double arr[100];

fptr = fopen(...);
...
while (fscanf(fptr, "%lf\n", &arr[i]) != EOF )
     i++;
...
0
 
brettmjohnsonCommented:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

struct dvector {
  double * vals;    /* array of doubles */
  size_t numVals;   /* number of values in vals */
  size_t maxVals;   /* size of current vals alloc */
};


int main(int argc, char ** argv)
{
  struct dvector dbls = {NULL, 0, 0};
  size_t i;
  char buff[1024];

  /* open the file */
  FILE * fin = fopen("readdbl.in", "r");
  if (!fin) {
    perror("Unable to open input file");
    exit(-1);
  }

  /* read each line, one double value per line */
  while (fgets(buff, sizeof(buff), fin)) {
    /* parse a double value from the line */
    char *endp;
    double num = strtod(buff, &endp);

    /* if we got a value, add it to the vector */
    if (endp != buff) {
      if (dbls.maxVals <= dbls.numVals) {
        /* if we ran out of slots in the current alloc, grow it */
        dbls.vals = (double *)realloc(dbls.vals, (dbls.maxVals += 100)*sizeof(double));
        if (dbls.vals == NULL) {
          perror("Unable to allocate memory");
          exit(-2);
        }
      }
      /* add our value to the vector */
      dbls.vals[dbls.numVals++] = num;
    }
  }
  fclose(fin);

  /* The dbls vector now contains your array of double values */
  for (i = 0; i < dbls.numVals; i++)
    printf("%g\n", dbls.vals[i]);

  exit(0);
}


0
 
smpoojaryCommented:
#include <stdio.h>
int main()
{
      FILE * fptr;
      int i;
      double arr[100];
      int nTotal;

      if((fptr = fopen("in.txt","r")) == NULL)
      {
            printf("ERROR: Unable to open input file\n");
            return 1;
      }

      i=0;
      while (fscanf(fptr, "%lf\n", &arr[i]) != EOF )
      {
            printf("%lf\n",arr[i]);
            i++;
      }
      nTotal = i;
      printf("Total number of lines = %d",nTotal);
      return 0;
}
0
 
furqanchandioCommented:
use fscanf to store the number in formatted form
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now