Question about Malloc and fscanf

This is a discussion on Question about Malloc and fscanf within the C Programming forums, part of the General Programming Boards category; Hi all, I have a very large file so I want to dynamically allocate the memory of a 2d array ...

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    9

    Question about Malloc and fscanf

    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.

    Code:
       FILE *fr;                                                                                // declare the file pointer 
       
       int x, y;                                                                            // Dimensions of .txt data, 3 columsn and 330770 inputs;
       
       //DYNAMIC MEMORY ALLOCATION FOR data ARRAY and finalData 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!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,271
    The correct format specifier of fscanf for double is %lf
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jul 2012
    Posts
    9
    You da man.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 05-19-2010, 03:12 AM
  2. Need help with fscanf question please
    By quickclick330 in forum C Programming
    Replies: 2
    Last Post: 12-12-2007, 02:29 PM
  3. fscanf question..
    By ozumsafa in forum C Programming
    Replies: 14
    Last Post: 07-26-2007, 01:31 AM
  4. fscanf question
    By panfilero in forum C Programming
    Replies: 2
    Last Post: 11-21-2005, 02:31 AM
  5. fscanf() question
    By Encrypted in forum C Programming
    Replies: 6
    Last Post: 01-20-2003, 01:14 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21