//Trying to read double values from each line of a file..and try to store them in arrays
//when i debugged,it doesnt do the parsing for the alldoubles
txt file consists

trying to put them in separate arrays

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

FILE *myFile;
unsigned int commas = 0;
//extracts the doubles from the file 
double *parseLine(char *line)
    unsigned int index = 0;
    char *head;
    double *values; 

    while(*head != '\0'){
       if(*head == ',')
    //there is commas + 1, # of doubles in the string 
    head = line;
    values = malloc(sizeof(double) * (commas + 1));
    while(head != NULL){
      head = strtok(line, ",");   
      values[index++] = atof(head);
    return values;

int main()
  char fileName[256];
  double **allDoubles; //pointer 2 pointer 2 double

  printf("Filename: ");
  //fgets(fileName, sizeof(fileName), stdin);
  //open file for reading
  if((myFile = fopen(fileName, "r")) == NULL){
    fprintf(stderr, "couldnt open %s", fileName);
    return -1;

  char buffer[BUFSIZ]; //going 2 hold the doubles as a String!!
  unsigned int nlines = 0; //# lines

  //we assume the file is ok, not corrupted data etc...
  while(fgets(buffer, sizeof(buffer), myFile) != NULL)
       if(strchr(buffer, '\n'))
  unsigned int j = 0; //index to *allDoubles  (i.e the list of doubles)
  allDoubles = malloc(sizeof(double *) * nlines);
  while(fgets(buffer, sizeof(buffer), myFile) != NULL)
        allDoubles[j++] = parseLine(buffer);

  printf("printing the doubles as double array ...\n");
  //backwards I dont want 2 make more variables ...lol 
 // while(--nlines >= 0){
    // while(--j >= 0)
	for(int i=0;i<nlines;i++){
		for(int k=0;k<j;k++){
      printf("%f ", allDoubles[nlines][j]);
  putchar('\n'); //new line
  printf("FINISHED ...\n");
  return 0;

Edited by WaltP: Added CODE tags -- with all the help about them, how could you miss using them????

Votes + Comments
Your lack of code tags is becoming a joke :(
7 Years
Discussion Span
Last Post by abhimanipal

One suggestion is to remove the data declarations from line 47-48 and put then at the beginning of the function.
In C all data declarations have to be at the beginning of the function

Put some printfs in your code, narrow down the area where you are getting the problem. Then post again on the forum


The compiler should have warned you about using an uninitialized variable ("head" is used at line 15 but was not set). Perhaps there is an option to enable warnings?
Line 12 needs to be:

char *head = line;

@ abhimanipal its not reaching line 60 the parsing ,its opening the file properly etc,it think it has to do something with the malloc on line 58


On the while loop in line 51,there are no brackets. Change that. Use the return value from malloc to see if you got the desired memory or not ?
Instead of using pointer to pointer, you could try using an array of pointers. The advantage being an array of pointers is slightly easier to manipulate.

Edited by abhimanipal: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.