Thread: How to convert a string file into a matrix in C

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

    How to convert a string file into a matrix in C

    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 manupulation bout couldnt here is my code


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define  ROW 5
    #define  COLS 11
    
    int main()
    {
     int mat[ROW][COLS];
     int i;int j; 
     FILE* fichier=NULL;
     fichier=fopen("swap.txt","r");
    
    
       for(i=0;i<ROW;i++)
       {
        for(j=0;j<COLS;j++)
       {
        fscanf(fichier,"%c",&mat[i][j]);
         printf("%c",mat[i][j]);
       }
    printf("\n");
       }
    
       }

    This in one among many other trial but I couldn t figure out how to solve the problem

    any help will be highly appreciated!!!!

    Best regards!!

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    If your file contents are just this:
    Code:
    **.*.*....*
    ..*.*...**.
    *...*.*....
    .*.*.*.*.*.
    ..*.*...*.*
    Then the following might work. You were forgetting about the newline character at the end of each line. One way to handle the newlines (and any other extra whitespace) is to simply put a space before the %c in your fscanf format, which causes it to skip whitespace (space, tab, newline, etc).
    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;
    }

  3. #3
    Registered User
    Join Date
    Nov 2016
    Posts
    33
    Thanks a million man! you truly saved my life
    May God bless your knowledge!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-19-2014, 07:32 PM
  2. Convert image to matrix
    By prefix in forum C++ Programming
    Replies: 1
    Last Post: 09-05-2011, 03:57 AM
  3. Replies: 5
    Last Post: 05-09-2010, 10:58 AM
  4. Convert data from file into a matrix
    By GARiMTO in forum C Programming
    Replies: 2
    Last Post: 11-22-2007, 02:17 PM
  5. How to convert char read from file into string
    By cruxxe in forum C Programming
    Replies: 7
    Last Post: 05-22-2002, 02:09 PM

Tags for this Thread