Thread: reading from an file into into an array!

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    39

    Question reading from an file into into an array!

    Hi I have a very very peculiar problem,

    I am trying to read a file with data in the following format

    1.0000 1 1 4 and storing into a 3d array as below:

    Code:
         
        file1= fopen("C:/Gibb.txt","r");
    
    	
        while(fscanf(file1,"%d %d %d %f",&filet,&filex,&filev,&filep)!=EOF)
    	 {
                          brown[filex][filet][filev]=filep;
                          
              }
         
         
         fclose(file1);
    the code compiles fine, but when running i get run time error after running some loops,

    P.S: the data file is really huge and has about 20000 rows

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    How did you declare your array?

    What ranges are filex etc, are they always in bounds (can you prove it?)

    Also, != EOF should be better written as == 4
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    "1.0000 1 1 4" and "%d %d %d %f" aren't really compatible.

  4. #4
    Registered User
    Join Date
    Jun 2009
    Posts
    39
    float brown[32][99][5] ; is how i declared my array

    ya the range of [filex] is 0-32
    [filet] is 0-99
    [filev] is 0-5

    @tabstop, sorry typo, the format is ' 1 1 4 1.000000'

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If the range of filex is 0-32, and 0-99, and 0-5, then you had better be declaring your array as brown[33][100][6].

  6. #6
    Registered User
    Join Date
    Jun 2009
    Posts
    39
    I tries this... but no avail.. still getting the same error

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You'll have to grin and bear it and tell us what the error actually is then I guess.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    [filex] is 0-32
    [filet] is 0-99
    [filev] is 0-5

    So add that to your code
    Code:
    if ( filex >= 0 && filex < 32 &&  and so on for t and v ) {
      brown[filex][filet][filev]=filep;
    } else {
      fprintf( stderr, "OUT of range: %d %d %d\n", filex, filet, filev );
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Jun 2009
    Posts
    39
    Hi,

    I tried some debugging, it is reading the file perfectly and storing the same into an array , however in the end of the mian function when returning Iam getting the error!!!!

    i.e "stack around brown was corrupted" please help
    Last edited by doubty; 07-01-2009 at 01:01 AM.

  10. #10
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> i.e "stack around brown was corrupted" please help

    Well, without seeing the code that you're using, it's impossible to say.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  11. #11
    Registered User
    Join Date
    Jun 2009
    Posts
    39
    Code:
    int main()
    {
        char inputexp[500];
        FILE *file1;
        int filet,filex,filev;
        float filep;
        float brown[33][99][5];
        int count=0,new_count=0,g;
    	clock_t start;
    
        file1= fopen("brown.txt","r");
    
    
        while(fscanf(file1,"%d %d %d %f",&filet,&filex,&filev,&filep)== 4)
    	 {
                          brown[filex][filet][filev]=filep;
                        
         }
         
        
         fclose(file1);
       
       
       	printf("enter expression");
    	scanf("%s",inputexp);
    	
    
        strcpy(label[0],inputexp);
                
    	
    	start = clock();
    	
    	
         
      
        parser(label);
        
    
    	 return 1;
              
      }
    please help!

  12. #12
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    1) You need to check the return value of fopen.
    2) You need to implement bounds checking (see Salem's post). It might be helpful to print the values read from the file to verify that they are correct, as well.
    3) I can't see how 'label' is declared, nor the code for 'parser', so I have no idea if they have anything to do with the error(s).
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  13. #13
    Registered User
    Join Date
    Jun 2009
    Posts
    39
    Hi,
    i have declared label as a global function hence it is nt shown, also the function parser works fine ( coz the code executes w/o any issues when the file operations are not done) , also the function has nothig to do with the file i.e the function parses the input provided by the user.

    I tried debug mode using an IDE, the file is opening, the contents are read fully and stored in an array, the file is closed, user is asked for the expression, the function is eveluated. return 1 is also executed, however after it reaches the last bracket and wants to stop the error is propped up.

    however
    Thanks

  14. #14
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> however after it reaches the last bracket and wants to stop the error is propped up.


    So why don't you post the updated code with the changes you've made?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  15. #15
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    try
    Code:
    double ***filep;
    filep = (double ***) malloc(33 * sizeof(double **));
    if (filep == NULL)
    { 
         printf("error in part 1\n");
         abort();
    }
    for (int i = 0; i < 33; i++)
    {
        filep[i] = (double **) malloc(100 * sizeof(double *);
        if (filep[i] == NULL)
        { 
         printf("error allocating memory for filep[%d]\n",i);
         abort();
        }
    }
    for (i = 0; i < 33; i++)
    {
         for (int j = 0; j < 100; j++)
         { 
              filep[i][j] = (double *) malloc(6 * sizeof(double));
              if (filep[i][j] == NULL)
              {
                   printf("Error allocating filep[%d][%d]",i,j);
                   abort();
              }
          }
    }
    I have had problems with passing arrays that are declared like you did before - might be completelt unrelated, but try mallocing it like that instead. Don't worry - you'll still be able to access array elements as you were before.

    If an array like you declared earlier gets too big, passing it as a parameter to a function starts causing problems, or at least it did to me. If you can use malloc, do it ^_^. Don't forget to free it with more loops.
    Last edited by KBriggs; 07-01-2009 at 07:29 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Reading from a file into an array
    By fmsguy06 in forum C Programming
    Replies: 6
    Last Post: 10-18-2008, 09:25 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Reading Characters from file into multi-dimensional array
    By damonbrinkley in forum C Programming
    Replies: 9
    Last Post: 02-24-2005, 01:31 PM