Home > database >  Why is my write function not writing properly to a .bin file?
Why is my write function not writing properly to a .bin file?

Time:03-21

I am writing a set of functions in order to write a binary file and then read it, and print contents. The struct must be 1 byte, and each member must store a number from 0-15 - fits into 8 bits total) in each(hardcoded by me in main()). The problem I am having is my write function does not store the proper numbers in the file it creates as the ones i hardcode into the struct. I've been trying to figure this out and i'm not sure what the issue is...I have another program that works just fine with very similar purpose. Please see my code below and the output I'm getting. Also I have a few print statements from trying to troubleshoot it.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

/* Purpose:
            */

struct score{
  //max 15 values for each
  //4 bits for player1 and 4 for player2
  unsigned char player1:4;
  unsigned char player2:4;

};

void printGG(struct score* game){
    printf("%hhu:%hhu\n", game->player1 , game->player2);
}

int writeGG(struct score* game, char * fileName){

    FILE *fp;
    if (!(fp=fopen(fileName,"wb"))){
      return 1;//file open error
    }
    //print to test the game pointer
    printf("WRITEGG() : player1: %d, player2: %d\n", game->player1, game->player2 );

    fwrite(&game ,sizeof(struct score) ,1 , fp);//write to file

    // check if fwrite failed
    if (fp == NULL){
        fprintf(stderr,"fwrite failed on %s\n",fileName);
        exit(EXIT_FAILURE);//can't write
    }
    fclose(fp);
    return 0;//normal termination
}

//reads provided .bin file and populates a score struct, then prints it.

int readGG(char *fileName)
{
    FILE *fp;
    if (!(fp=fopen(fileName,"rb"))){
        fprintf(stderr,"failed to open %s\n",fileName);
        exit(EXIT_FAILURE);//can't open
    }
    struct score s;
    if(fread(&s ,sizeof(struct score) ,1 , fp)!=1) return 2;//read error
    printf("readGG() : player1: %d, player2: %d\n", s.player1, s.player2 );
    fclose(fp);
    printf("%hhu:%hhu\n",s.player1,s.player2);
    return 0;//normal termination
}



int main()
{
    //create the score struct
    struct score scores;

    //hard code the scores
    scores.player1 = 10;
    scores.player2 = 1;
    printf("      player 1 has: %d\n",scores.player1);
    printf("      player 2 has: %d\n",scores.player2);

    //print the score in the format score:score
    printGG(&scores);

    //write to bin file
    writeGG(&scores, "Score.bin");

   //read file 
    readGG("Score.bin");

    
    return 0;
}

I feel like it could just be a small error that I'm missing but I just don't see it.

//Output for the main() when ran:

      player 1 has: 10
      player 2 has: 1
10:1
WRITEGG() : player1: 10, player2: 1
15:1

//here the .bin file contains just the letters 1F...which is 15 and 1,
//not 10 and 1 which i have entered.

CodePudding user response:

You are wrting out the address of the struct score *game

 fwrite(&game ,sizeof(struct score) ,1 , fp);//write to file

game is already a pointer to struct score. You need

fwrite(game ,sizeof(struct score) ,1 , fp);//write to file
  •  Tags:  
  • c
  • Related