Code:
#include <stdio.h>
unsigned char arr[] = {
1,1,5,1,3,2,
2,6,9,2,7,8,
3,2,8,3,4,6,3,6,1,3,7,5,
4,2,5,
5,1,3,5,5,1,5,8,6,5,9,5,
6,3,9,6,5,3,6,8,4,6,9,1,
7,3,4,7,5,6,7,7,1,
8,5,8,
9,4,3,9,5,4,9,6,2,9,8,8
};
int main (int argc, char **argv) {
FILE *fp;
fp = fopen ("initial.dat", "wb");
fwrite (arr, 3, 44, fp);
fclose (fp);
}
I would like to make a program just read the file (dynamically) and assign the values to an array[9][9] .
Code:
#include <stdio.h>
#include <stdlib.h>
struct Sudoku
{
char row;
char col;
char value;
} sud;
int main (int argc, char **argv)
{
int A[9][9];
FILE *fp;
fp = fopen("initial.dat", "rb");
if(fp == NULL)
{
printf("Error opening file\n");
exit(1);
}
printf("Testing fread() function: \n\n");
while( fread(&sud, sizeof(sud), 1, fp) == 1 )
{
printf("%hd ", sud.row);
printf("%hd ", sud.col);
printf("%hd\n", sud.value);
A[sud.row-1][sud.col-1]=sud.value;
printf("==>%hd",A[sud.row-1][sud.col-1]);
}
fclose(fp);
return 0;
}
But the output is not as expected :