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:
#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:
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