Untitled

 avatar
unknown
c_cpp
a year ago
681 B
8
Indexable
double *ReadDataFromFile(const char *fileName, int *numPoints) {
  FILE *fileIn = fopen(fileName, "r");
  *numPoints = 0;
  char *line = NULL;
  size_t len = 0;
  // Read how many points
  while (getline(&line, &len, fileIn) != -1)
    *numPoints = *numPoints + 1;
  // Restart file
  rewind(fileIn);
  // free line
  if (line)
    free(line);
  // reserve memory for points
  double *points = (double *)malloc(*numPoints * 2 * sizeof(double));
  // Read points
  for (int i = 0; i < *numPoints; ++i) {
    double x, y;
    fscanf(fileIn, "%lf\t%lf\n", &x, &y);
    printf("Read coordinate {%g,%g}\n", x, y);
    points[i * 2] = x;
    points[i * 2 + 1] = y;
  }
  return points;
}
Editor is loading...
Leave a Comment