Thread: Problem with reading a string matrix from a file

  1. #1
    Registered User
    Join Date
    Nov 2016
    Posts
    33

    Problem with reading a string matrix from a file

    Good day C Gurs

    I have a tring file called "Swap.txt" its content is the following matrix
    char mat[ROW][COLS]=

    "**.*.*....*",
    "..*.*...**.",
    "*...*.*....",
    ".*.*.*.*.*.",
    "..*.*...*.*"


    i want to convert it into a matrix in C for further manipulation and I used two codes for that

    the first code is fine and give satisfactory results

    Code:
    
    
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define ROWS 5
    #define COLS 11
    #define FILENAME "swap.txt"
    
    int main() {
        char m[ROWS][COLS]; // should presumably be a char array
        int r, c;
        FILE *f;
    
        f = fopen(FILENAME, "r");
        if (f == NULL) {
            printf("Can't open %s.\n", FILENAME);
            exit(EXIT_FAILURE);
        }
    
        // read in the matrix
        for (r = 0; r < ROWS; r++)
            for (c = 0; c < COLS; c++)
                fscanf(f, " %c", &m[r][c]); // note the extra space
    
        // print out the matrix
        for (r = 0; r < ROWS; r++) {
            for (c = 0; c < COLS; c++)
                printf("%c", m[r][c]);
            printf("\n");
        }
    
        return 0;
    }


    The second code


    Code:
    
    
    Code:
    f=fopen("filename","r");
    
    
     while(fgets(buf,row,f)!=NULL)
     {
         for(i=0;i<row;i++)
         {
             for(j=0;j<col;j++)
            {
    
    
      sscanf(buf,"%c", &mat[i][j])  ;
    
    
             }
    
    
    
    
            }
    
    
     }
    for(i=0;i<row;i++)
         {
             for(j=0;j<col;j++)
            {
    
    
      printf("%c", mat[i][j])  ;
    
    
             }
    printf("\n");
    
    
            }
    
    
    
    
     }



    give me a matrix
    ...........
    ...........
    ...........
    ...........
    ...........


    i see no stars here *
    what is wrong with the second code?
    Many thanks in advance


  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > sscanf(buf,"%c", &mat[i][j]) ;
    This doesn't walk through buf extracting each char in turn.
    You just get multiple copies of buf[0]
    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
    Registered User
    Join Date
    Nov 2016
    Posts
    33
    Quote Originally Posted by Salem View Post
    > sscanf(buf,"%c", &mat[i][j]) ;
    This doesn't walk through buf extracting each char in turn.
    You just get multiple copies of buf[0]
    Thanks a million Salem !

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with matrix reading from a file
    By kimblie in forum C Programming
    Replies: 4
    Last Post: 08-30-2016, 02:00 PM
  2. Reading txt file to a MATRIX
    By Cosmo S in forum C Programming
    Replies: 2
    Last Post: 01-11-2013, 04:40 AM
  3. 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
  4. Reading char matrix from file
    By TriKri in forum C Programming
    Replies: 5
    Last Post: 07-19-2006, 02:05 AM
  5. problem reading string from file
    By seitan in forum C Programming
    Replies: 2
    Last Post: 01-19-2005, 03:13 AM

Tags for this Thread