Thread: Loading data into a dynamic array

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    115

    Loading data into a dynamic array

    Hi!

    I am reading data from a text file into a dynamically constructed array, like this:

    Code:
    static const unsigned pN = 1 << 13;
    
    float *p = NULL;
    p = new float[ pN ];
    
    // DATA
    FILE *file;
    
    file = fopen( "dist.txt" , "r" );
    
    unsigned int i = 0;
    
    while( !feof(file) ) {
        fscanf( file, "%f", p[ i ] );
        i++;
    }
    
    fclose(file);
    This works fine for arrays of float but does not work when I change the type to double? Any idea why, and how to fix it?

    Thanks for you help!
    Serge
    Last edited by serge; 06-18-2009 at 02:44 PM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Are you also changing %f to %lf to indicate that you mean double?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by serge View Post
    This works fine
    No, it doesn't work at all.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    float arrp[ pN ];
    What's the purpose of this line? With doubles it takes twice the amount of stack memory and it is obviously two much - the stack is just a few megabytes for your program.

    BTW, in C++ you'd use std::vector to avoiding a) overallocating way too much, b) still running out of bounds in the file has more data than that.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    115
    Quote Originally Posted by matsp View Post
    Are you also changing %f to %lf to indicate that you mean double?

    --
    Mats
    Thanks! This fixed the problem in fscanf.

    BTW: What is the difference between %f and %lf in printf?

  6. #6
    Registered User
    Join Date
    May 2008
    Posts
    115
    Quote Originally Posted by anon View Post
    Code:
    float arrp[ pN ];
    What's the purpose of this line? With doubles it takes twice the amount of stack memory and it is obviously two much - the stack is just a few megabytes for your program.

    BTW, in C++ you'd use std::vector to avoiding a) overallocating way too much, b) still running out of bounds in the file has more data than that.
    Sorry for the confusion, this line was superflous, just deleted it in edit

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by serge View Post
    Thanks! This fixed the problem in fscanf.
    And yet the code you posted is still wrong. That isn't the real code, is it?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  8. #8
    Registered User
    Join Date
    May 2008
    Posts
    115
    Quote Originally Posted by brewbuck View Post
    And yet the code you posted is still wrong. That isn't the real code, is it?
    ok, here is code that works
    Code:
    #include <iostream>
    
    int main()
    {
    static const unsigned N = 13;
    static const unsigned pN = 1 << N;
    
    double *p = NULL;
    p = new double[ pN ];
    
    unsigned int i;
    
    FILE *file;
    file = fopen( "dist.txt" , "r" );
    
    i = 0;
    
    while( !feof(file) ) {
        fscanf( file, "%lf", &p[ i ] );
        i++;
    }
    fclose(file);
    
    system( "PAUSE" );
    return 0;
    }

  9. #9
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268

  10. #10
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by serge View Post
    ok, here is code that works
    And the missing '&' appears!
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Array substitution in composite data variable
    By DavidDobson in forum C Programming
    Replies: 2
    Last Post: 08-12-2008, 04:29 AM
  2. linked list inside array of structs- Syntax question
    By rasmith1955 in forum C Programming
    Replies: 14
    Last Post: 02-28-2005, 05:16 PM
  3. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM