Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Reading File Contents In C into Matrix

Options
  • 18-02-2005 11:47pm
    #1
    Closed Accounts Posts: 1,541 ✭✭✭


    Hello Everyone,

    I am trying to read in from a file ic C. I want to read in the contents from a file into a 8 by 55 matrix/multi dimensional array.

    Below is Matlab Code that does this for me. It produces a 8*55 Matrix for the file in question.
    testname=sprintf('U:\\commands\\c%dr%d.warp ',j,i);
    testpointer=fopen(testname,'rb');
    testfile=fread(testpointer,[8,inf],'double');
    fclose(testpointer);
    

    testfile contains the file contents and forms an 8*55 matrix with the correct values.

    How do I do this in C.?


    Even if I cannot read the file into a Matrix, I would at least like access to each column of the matrix in the file to perform interpolation on a column by column basis.

    Any Advice appreciated!

    Finnpark


Comments

  • Registered Users Posts: 95 ✭✭Mr.StRiPe


    This should do the job!
    #include <stdio.h>
    
    double TestFile[8][55];
    FILE *stream;
    
    void main()
    {
       
       if( (stream = fopen( "matrix.txt", "r+t" )) != NULL )
       {
          fread( TestFile, sizeof(TestFile), 1, stream );
          
          fclose( stream );
       }
       else
          printf( "File could not be opened\n" );
    }
    


  • Closed Accounts Posts: 1,541 ✭✭✭finnpark


    Thanks for that , I will try it first thing Monday Morning.

    I will be reading in a binary file so I presume I change the t to b?

    Also what does the 1 stand in the third column?

    Thanks again, Finnpark :)


  • Registered Users Posts: 7,276 ✭✭✭kenmc


    finnpark wrote:
    Also what does the 1 stand in the third column?

    It means read 1 unit of size TestFile. If you wanted to read eg 4 characters instead you'd do
    fread(SomeCharBuffer, sizeof(char), 4, TheFile)


  • Closed Accounts Posts: 1,541 ✭✭✭finnpark


    Thanks Ken,

    That does the trick just great.

    Now I have 2 more questions(sorry :) ).

    (1) Instead of TestFile being a constant array of 8*124 I want the 124 part to vary. Not all files I am reading in will be 8*124. Some will be 8*34 or 8*88. The 124 part should not be constant. The 124 part should be equal to file_length/64.

    I need to be able to find out the dimensions of the matrix and store contents in array exactly that size. Can I use dynamic allocation of memory to do this?

    eg
    TestFile = new double[8,num_frames];
    


    (2) Iam getting an error else where for the following line:
    newy[n2][ny]=oldy[count] + newx[ny];// - (oldx[count])) * ((oldy[count+1]) - (oldy[count]))); // Interpolation
    


    The error reads :mad: :

    error C2111: pointer addition requires integral operand

    Any ideas, and many thanks again.

    Finn


Advertisement