-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?
– G. Bittencourt
prints trash instead of txt content
– Diogo Cruz
getGrid(filename, N, N, mat); the N ta declared where? You can post all the code?
– Valmor Flores
You have defined your function
getGrid
as returning aint
but is returning aint **
.– anonimo
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.
– anonimo
@Valmorflores N=9
– Diogo Cruz
Yes, I imagined. Look at the answer, I divided it into two LIN and COL ;)
– Valmor Flores