How to read a txt matrix

Asked

Viewed 168 times

-1

I needed to print a matrix I have on txt in the terminal.

This is my txt:

010009070
300001000
070080000
807000000
050076000
064000510
030200000
092400000
000000023

What I’ve already tried:

int getGrid(char *filename, int lines, int columns, int **mat) {

    FILE *fp;
    fp = fopen(filename, "r");

    for (int i = 0; i < lines; ++i) {
        for (int j = 0; j < columns; ++j) {
            fscanf(fp, "%d", &mat[i][j]);
        }
    }

    fclose(fp);
    return mat;

}
main(){
 int **mat = malloc(N * sizeof(int *));
    for (int i = 0; i < N; ++i) {
        mat[i] = malloc(N * sizeof(int));
    }
    //fillGridRandom(&row, &col, 10);
    char *filename = "C:/SudokuX/InitTab.txt";
    getGrid(filename, N, N, mat);
}
  • But what’s the problem?

  • prints trash instead of txt content

  • getGrid(filename, N, N, mat); the N ta declared where? You can post all the code?

  • You have defined your function getGridas returning a int but is returning a int **.

  • I don’t know how many columns you expect to read in each row, since you didn’t report the value of N, but the way you displayed your file each row has a single integer.

  • @Valmorflores N=9

  • Yes, I imagined. Look at the answer, I divided it into two LIN and COL ;)

Show 2 more comments

1 answer

0

Hello! I made in a single block your proposal. Then divide into function as you wish. Code:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

#define LIN 9
#define COL 9

int main()
{

    int **mat ;
    int i, j ;

    int dado;
    FILE *fp;

    fp = fopen ("tabela.txt", "r+");
    mat = ( int ** ) malloc (LIN * sizeof (int*)) ;
    for (i=0; i < LIN; i++)
    mat[i] = ( int * ) malloc (COL * sizeof (int)) ;
    for (i=0; i < LIN; i++)
    {
        for (j=0; j < COL; j++)
        {
        dado = fgetc(fp);        
        mat[i][j] = dado ;
        }
    }

    printf("\nMatriz:\n");
    for(i=0;i<LIN;i++)
    {

       for(j=0;j<COL;j++)
       {
           if ( mat[i][j] == 10 )
           {
               printf("\n");
           }
           else
           {
               /* code */
               printf("%3d\t", mat[i][j]-48);
           }
       }
    }

    // libera a memória da matriz
    for (i=0; i < LIN; i++)
       free (mat[i]) ;
    free (mat) ;

}

But without malloc there are simpler options. Including in a proposal already discussed here: Passing information from a file to an array

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

#define NLIN 9
#define NCOL 9

void lerMatriz(FILE *arquivo, char out[NLIN][NCOL]){
    int c;
    for(int i=0; i<NLIN; i++){
        for(int j=0; j<NCOL; j++){
            c = fgetc(arquivo);
            if(c == EOF){ /*ERRO*/ }
            out[i][j] = c;
        }
        c = fgetc(arquivo);
        if(c != '\n'){ /*ERRO*/ }
    }
}

int main(){
    FILE *entrada = fopen("tabela.txt", "r");

    char tabuleiro[NLIN][NCOL];
    lerMatriz(entrada, tabuleiro);


    for(int i = 0; i < NLIN; i ++){
        for(int j = 0; j < NCOL; j++){
            printf("%c", tabuleiro[i][j]);
        }
        printf("\n");
    }

    fclose(entrada);
    return 0;
}

Copy in repository: https://github.com/valmorflores/matriz_c_malloc.git

  • How, it seems to me, he desires a matrix of integers, not char, here: dado = fgetc(fp); should be: dado = fgetc(fp) - '0';

Browser other questions tagged

You are not signed in. Login or sign up in order to post.