Thread: Question about Malloc and fscanf

  1. #1
    Registered User
    Join Date
    Jul 2012

    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.

       FILE *fr;                                                                                // declare the file pointer 
       int x, y;                                                                            // Dimensions of .txt data, 3 columsn and 330770 inputs;
       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.

    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
    The correct format specifier of fscanf for double is %lf
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

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

Popular pages Recent additions subscribe to a feed

Similar Threads

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

Tags for this Thread

Website Security Test