0
I have to read an array 48X2 by a file .txt
, the file has this structure:
nome_da_pessoa
1 2
3 4
...
N N
Where N is the 47 position elements, I need to save the person’s name in a variable, and save the matrix in another variable, to perform this task, I did the following algorithm:
#include<stdio.h>
#include<stdlib.h>
#define Lin 48
#define Col 2
int main(){
int i, j, Mat[Lin][Col];
char nome[30];
FILE *arq;
arq=fopen("matriz.txt", "r");
fgets(nome, 30, arq);// pega o nome do participante
for(i=0;i<Lin;i++){
for(j=0;j<Col;j++){
fscanf(arq,"%d ", &Mat[i][j]);
printf("%d ", Mat[i][j]);//testar se a impressão esta correta
}
}
fclose(arq); //fechar arquivo
return 0;
}
However, when I print the variable Mat[i][j]
the program printed the matrix with the numbers on each other, or instead of 48x2. I would like to know what my mistake, and what I should do.
After the
}
that closes thefor
internal, addprintf("\n");
– Marcelo Shiniti Uchimura
You don’t even have to do
"%d "
, with this final blank, in thefscanf()
because white spaces are ignored by the function– Marcelo Shiniti Uchimura
Ah, got it, I was putting the
printf("\n")
inside the internal, but there was a number below the other, thank you.– user117001