1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
#include <stdio.h>
unsigned char arr[] = {
1,4,9,1,5,2,1,6,3,
2,2,4,2,3,2,2,4,7,2,6,8,2,7,3,2,8,5,
3,2,7,3,4,5,3,5,4,3,6,1,3,8,9,
4,1,6,4,2,9,4,3,1,4,7,2,4,8,3,4,9,8,
5,1,2,5,3,7,5,7,4,5,9,5,
6,1,5,6,2,3,6,3,4,6,7,9,6,8,1,6,9,7,
7,2,2,7,4,1,7,5,5,7,6,7,7,8,4,
8,2,5,8,3,9,8,4,8,8,6,4,8,7,1,8,8,2,
9,4,2,9,5,9,9,6,6
};
int main() {
// To write arr data
FILE *fp = fopen("Test.dat", "wb");
fwrite(arr, sizeof *arr, sizeof arr / sizeof *arr, fp);
fclose(fp);
// Read data into 9 by 9 matrix
int board[9][9] = {0};
FILE *f = fopen("Test.dat", "rb");
unsigned char row, col, val;
while (fread(&row, 1, 1, f) == 1) {
fread(&col, 1, 1, f);
fread(&val, 1, 1, f);
board[row-1][col-1] = val;
}
fclose(f);
for (int r = 0; r < 9; r++) {
for (int c = 0; c < 9; c++)
printf("%d ", board[r][c]);
putchar('\n');
}
return 0;
}
|