Hi all,

I have a very large file so I want to dynamically allocate the memory of a 2d array and then fill it up with the file inputs. Before i used malloc to make the 2d array, everything worked fine (I was using a practice small .txt). However, i believe something is going wrong with my fscanf part and I don't know why.

   FILE *fr;                                                              // declare the file pointer 

   int x, y;                                                              // Dimensions of .txt data, 3 columsn and 330770 inputs;

   //DYNAMIC MEMORY ALLOCATION FOR data ARRAY

   double** data; 
   data = (double **) malloc(330770 * sizeof(double*)); 
   for (int h = 0; h < 330770; h++)  
            data[h] = (double*) malloc(3*sizeof(double));    
                                                                         //determines the placement of the finalData
   fr = fopen ("c:\\blahblah.txt", "r");                                 /* reads the file */                                 
   for(x=0; x<12; x++){                                                  // loops through the rows
                     y = 0;                                              //starts at first column during each row loop        
                     while(fscanf(fr,"%L", &data[x][y++]) > 0);          // loops through the columns until each row ends.
   }

    cout<<data[0][0]; 

Currently, it is displaying 1.7317e-307 instead of displaying the correct number of 20010929. Thankyou!

Problem is that I used %L instead of %lf.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.