Thread: Read binary file with numbers

  1. #1
    Registered User
    Join Date
    Aug 2018
    Posts
    2

    Read binary file with numbers

    Hi all I have generate a initial state of a sudok using the following code.
    Every triplet is translated to row,column,value.
    For example : 1,1,5 means row=1 column=1 value=5

    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] .
    So, after the read
    array[0][0]=5
    array[0][2]=2
    array[1][5]=9
    ...
    array[8][7]=8

    I am trying with the following code :
    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 :
    Testing fread() function:
    1 1 5
    ==>51 3 2
    ==>22 6 9
    ==>92 7 8
    ==>83 2 8
    ==>83 4 6
    ==>63 6 1
    ==>13 7 5
    ==>54 2 5
    ==>55 1 3
    ==>35 5 1
    ==>15 8 6
    ==>65 9 5
    ==>56 3 9
    ==>96 5 3
    ==>36 8 4
    ==>46 9 1
    ==>17 3 4
    ==>47 5 6
    ==>67 7 1
    ==>18 5 8
    ==>89 4 3
    ==>39 5 4
    ==>49 6 2
    ==>29 8 8
    ==>8

    Any help please ?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    Your fwrite is writing a lot more than you expect.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Also posted here.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-10-2013, 09:47 PM
  2. Replies: 12
    Last Post: 06-18-2012, 08:23 AM
  3. Read ints from a binary file that has 2560 numbers in it
    By jackofalltrades in forum C Programming
    Replies: 10
    Last Post: 06-15-2012, 08:33 AM
  4. How to read numbers out of a file in c
    By muskateer1 in forum C Programming
    Replies: 3
    Last Post: 04-05-2012, 02:29 PM
  5. Read in numbers from file
    By ElNino in forum C Programming
    Replies: 1
    Last Post: 02-29-2012, 01:47 PM

Tags for this Thread