Thread: How to read from txt file to a matrix

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    5

    How to read from txt file to a matrix

    Hello,

    I need to read list of numbers form a txt file to be a matrix in my programe. the matrix i need it to be 3x3 matrix.
    thanks for any help

    the list of numbers are as follows:

    116.068360
    -124.401694
    8.333333
    -124.401694
    133.333333
    -8.931640
    8.333333
    -8.931640
    0.598306

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    What have you tried?

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    5
    I tryed the following:
    but it give me the first entry of the matrix onle, i am quite new to programming
    Code:
    #include<stdio.h>
    
    main()
    {
        FILE *arr;
        float k;
        arr =fopen("SKelem.txt","r");
        fscanf(arr, "%f", &k);
        printf("%f
    ", k);
    }

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    5
    Thanks alot for your response

    I know that I should i dentify k as an array but it gives me error.
    I am quite new in programming and I should do a big project and this should be written in C.
    I hope if I could have some help here?

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Well if it is right for the first element you can do it for all of them. Use a loop to read from the whole file. When you get all of the input on screen, then you can try shaping it into a matrix.

  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    5
    I dont know how to do it?
    how to do a loop to read from the whole file?
    should i define k as a matrix (double k[3][3])?

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    how to do a loop to read from the whole file?
    Loop the fscanf() statement until it fails to convert something. fscanf() returns a number to the caller that means how many items from the file were successfully converted into data, or EOF if the file is at the end. You can use that number to create a loop condition.
    should i define k as a matrix (double k[3][3])?
    It's probably a good idea. I would read a single number into float k; and then assign k somewhere in float matrix[3][3]; That's actually pretty easy. Just make sure that the subscripts for the row and column are within the bounds of the matrix.

  8. #8
    Registered User
    Join Date
    Nov 2011
    Posts
    5
    Thank you very much finally it works
    I did the following and it works?
    thanks alot.

    Code:
    #include<stdio.h>
    
    main()
    {
        FILE *arr;
        float k[3][3]={0.0};
        arr =fopen("SKelem.txt","r");
        int i,j;
        
        
        for(i=0 ;i<3; i++)
        {
            for(j=0; j<3; j++)
            {
                fscanf(arr, "%f%*[^\n]%*c",&k[i][j]);
                printf("%f\n", k[i][j]);
            }
        }
    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. read image to 2d matrix???
    By sukuku in forum C++ Programming
    Replies: 3
    Last Post: 10-10-2011, 06:06 AM
  2. Read an image to Matrix in visual c++ 6.0???
    By sukuku in forum C++ Programming
    Replies: 6
    Last Post: 10-03-2011, 09:07 AM
  3. Read ASCII file into matrix in C++
    By abotaha in forum C++ Programming
    Replies: 1
    Last Post: 11-02-2010, 10:49 AM
  4. MPI read matrix malloc problem
    By Chris_1980 in forum C Programming
    Replies: 1
    Last Post: 06-10-2010, 03:48 AM
  5. Read Image to a Matrix
    By scrapedbr in forum C Programming
    Replies: 3
    Last Post: 05-02-2003, 03:53 PM