Thread: Help with matrix reading from a file

  1. #1
    Registered User
    Join Date
    Aug 2016
    Posts
    1

    Help with matrix reading from a file

    Hello I am having trouble to get desired output for reading a 3x3 matrix from a data file
    The code is::
    insert
    Code:
        FILE *arr;
        float k[3][3]={0.0};
        arr =fopen("data11.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]);
            }
    and data file is::
    0 5 10 54 57 58 90 45
    2 0 15 1 12 12 0 84
    1 3 0 87 48 0 45 9
    4 58 4 0 84 95 78 12

    output should be ::
    0 5 10
    2 0 15
    1 3 0

    instead its showing::
    0 5 10
    54 57 58
    90 45 2

    whats the problem here? I am a novice in programming so if anyone can help I would much appriciate it.Thanks in advance.
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Your question is extremely deceptive since you've invented data that you didn't actually use. Your actual data file is more like this:
    Code:
    1 2 3 4 5 6
    2 ...
    3 ...
    4 ...
    5 ...
    6 ...
    I.e., it has the same values going down the first column as are in the first row.
    Your program is simply reading the first value from each row, as per your fscanf format.
    If you want to read the first three, try:
    Code:
        for (i = 0; i < 3; i++)
            fscanf(arr, "%f%f%f%*[^\n]%*c", &k[i][0], &k[i][1], &k[i][2]);
    Or
    Code:
        for (i = 0; i < 3; i++) {
            for (j = 0; j < 3; j++) {
                fscanf(arr, "%f", &k[i][j]);
            }
            fscanf(arr, "%*[^\n]%*c");
        }

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    505
    scanf and newlines are a nuisance.
    Basically the scanf family of functions treat newlines like white space. There are ways of coaxing better behaviour out of them, but it turns the format string into almost a mini-program in its own right, which needs debugging.
    Better use fgets with a nice big buffer, say 1024 bytes, to read it a whole line.
    Then you want the first three values and to discard the rest. So

    Code:
        N = sscanf(buff, "%f %f %f", &a, &b, &c);
        if(N == 3)
        {
           ///Ok, a b and c are the values
        }
        else
        {
           //not Ok. Input corrupt? 
        }
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Given your input file consists only of numbers, with whitespace and newlines, you can dispense with the "%*[^\n]%*c" parts.

    %f by itself will happily skip the whitespace and newlines.

    Also, if you intend to write C, then make sure your source file is foo.c, not foo.cpp.
    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.

  5. #5
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by Salem View Post
    Given your input file consists only of numbers, with whitespace and newlines, you can dispense with the "%*[^\n]%*c" parts.
    My understanding is that he only wants to read a certain number of values (e.g., 3) from each line and skip the rest of the line.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading txt file to a MATRIX
    By Cosmo S in forum C Programming
    Replies: 2
    Last Post: 01-11-2013, 04:40 AM
  2. reading a matrix and printing out the matrix
    By Lina_inverse in forum C Programming
    Replies: 9
    Last Post: 10-23-2012, 04:09 PM
  3. Reading a binary file like a matrix of ints
    By afflictedd2 in forum C++ Programming
    Replies: 3
    Last Post: 01-11-2011, 07:46 AM
  4. Reading a binary file like a matrix of ints
    By afflictedd2 in forum C++ Programming
    Replies: 3
    Last Post: 06-26-2010, 09:45 AM
  5. Reading char matrix from file
    By TriKri in forum C Programming
    Replies: 5
    Last Post: 07-19-2006, 02:05 AM

Tags for this Thread