Read .dat file

Aug 21, 2018 at 4:53pm
Hi all, I am new in programming and I have found the following code in order to create a s'udoku initial state.

How can I read the generated file in C ?
Could you help me please ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#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 (int argc, char **argv)
{
   
    FILE *fp;
    
    fp = fopen ("Test.dat", "wb");
    fwrite (arr, 3, 44, fp);
    fclose (fp);
}


I am trying something as follow. But it is not printable in the screen :
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
#include <stdio.h>

int main(){
  char str[40];
  int i=40;
  int linecount = 0;
  FILE *myfile;

  /* open the file for reading */
  myfile = fopen("Test.dat","r");

  /* make sure the open was successful */
  if(myfile == NULL){
    printf("Error opening file\n");
    return 0;
  }

  /* read a string from the file */
  while(fgets(str,i,myfile) != NULL){
    printf("%s\n",str);
    linecount++;
  }

  fclose(myfile);

  return 1;
}
Aug 21, 2018 at 5:29pm
Your second parameter to fwrite is wrong. It should be the size of the unsigned char.
Also since you use fwrite the logical step would be to use fread to read the file
A normal Soduko board has only 81 fields. Why does you arr have more?
Aug 21, 2018 at 7:34pm
The numbers above are given with the logic that every three digits a new coordinate with value is given.
For example 1,4,9 it means 1:row 4:column 9:value
then 1,5,2 it means 1:row 5:column 2:value
etc..

Could you help me please just read the file ?
Last edited on Aug 21, 2018 at 7:34pm
Aug 22, 2018 at 7:09am
This will read the file into an array.
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
42
43
44
45
46
47
48
#include <stdio.h>
#include <stdbool.h> // C99

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 
};

#define NUM_FIELDS  sizeof(arr) / sizeof(arr[0])

int main()
{
  FILE *fp = NULL;
  bool error = false;

  printf("Start writing file....");
  fp = fopen("Test.dat", "wb");
  fwrite(arr, sizeof(unsigned char), sizeof(arr), fp);  
  fclose(fp);
  printf("finished");
  
  unsigned char input[NUM_FIELDS] = {0};
  printf("\n\nReading file...");
  fp = fopen("Test.dat", "rb");
  fread(input, sizeof(unsigned char), NUM_FIELDS, fp);

  // checking if we read everything correctly
  for (int i = 0; i < NUM_FIELDS; i++)
  {
    if (input[i] != arr[i])
      error = true;
  }
  fclose(fp);
  if (error)
    printf("\nError reading file...");
  else
    printf("\nFile read correctly...");

  return 0;
}
Aug 22, 2018 at 9:37am
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;
}


Output:
0 0 0 9 2 3 0 0 0 
0 4 2 7 0 8 3 5 0 
0 7 0 5 4 1 0 9 0 
6 9 1 0 0 0 2 3 8 
2 0 7 0 0 0 4 0 5 
5 3 4 0 0 0 9 1 7 
0 2 0 1 5 7 0 4 0 
0 5 9 8 0 4 1 2 0 
0 0 0 2 9 6 0 0 0 
Last edited on Aug 22, 2018 at 9:38am
Aug 24, 2018 at 4:03pm
Thanks very much for your answers.

I tried the folowing and I get correctly the values

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
struct Sudoku
{
    char row;
    char col;
    char value;
} input;

void readFile(char *file_name) {

    FILE *fp = fopen(file_name, "rb");

    while( fread(&input, sizeof(input), 1, fp) == 1 )
        sudoku[input.row-1][input.col-1]=input.value;
    
    fclose(fp);
}


But I think your code is much better.

Thanks again
Topic archived. No new replies allowed.