Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help: Reading a U8 from binary file?

Status
Not open for further replies.

csteinhilber

Programmer
Aug 2, 2002
1,291
US
I'm developing a game I need to save a gamestate for.
At this stage, the var I need to save out is stored as a U8:

Code:
  u8 completedLevel;

When I save out, I'm doing:
Code:
  file = fopen("save.bin", "wb");
  if(file != NULL) {
       nb = fwrite(&completedLevel, 1, sizeof( u8 ), file);
       fclose(file);
  }

And when I read it back in, I'm trying:
Code:
  file = fopen("save.bin", "rb");
  if(file != NULL) {
       fread(&completedLevel, 1, sizeof( u8 ), file);
       fclose(file);
  }

But somewhere between saveout and readin, the value gets munged. I'm guessing it's because the datatype isn't right.

Anyone have any pointers on what I'm doing wrong?

Thanks in advance!
-Carl
 
Are you sure that fwrite and/or fread were performed successfully? You don't print any error messages on possible open/fwrite/fresd failures.
The 2nd parameter of fread/fwrite is item size in bytes so it's more natural way to write
C:
nb = fwrite(&completedLevel, sizeof( u8 ), 1, file);
if (nb != 1) {
    /* write failed */
}
... /* do the same for fread... */
However if you know that "somewhere between saveout and readin, the value gets munged" then i/o operations don't bear a relation to your troubles...

The last reason: u8 is a very exotic type (where is its declaration?). But it's the other story...

 
Thanks for the response, ArkM.

Sorry, should've explained... U8 is defined in my toolchain. It's simply an unsigned int8. Not terribly exotic, I wouldn't think.

So, I was originally trying to fwrite/fread a struct, but recognized that I wasn't getting out what I put in, so I slimmed down the model to only saving a u8 just to try to figure out what's going on. I do check that both the write and read are successful (they are).

Here's my original tests with the struct:
Code:
   struct GameState {
      u8 currentLevel;
      u8 completedLevel;
      u8 score;
      bool cheatsactive;
   };

     :
     :


   void GamePers::WriteGameState() {
        FILE *file=NULL;
	s8 fr;
	GameState writeGameState;

	writeGameState.currentLevel=9;
	writeGameState.completedLevel=28;
	writeGameState.score=2000;
	writeGameState.cheatsactive=false;

        file = fopen("save.bin", "wb");
        if(file != NULL) {
          fr=fwrite(&writeGameState, sizeof(GameState), 1, file);
          printf ("write fr is: %d", fr);
          if(fr != 1){
              printf ("wrote currentLevel of: %d", writeGameState.currentLevel);
              // Assert
          }

          fclose(file); 
        }
   }

   // write fr is always 1, and "wrote currentLevel" reports 9 as expected.


   void GamePers::ReadGameState() {
        FILE *file=NULL;
	s8 fr;
	GameState readGameState;

        file = fopen("save.bin", "rb");
        if(file != NULL) {
          fr=fread(&readGameState, sizeof(GameState), 1, file);
          printf ("read fr is: %d", fr);
          if (fr != 1{
             printf ("read currentLevel of: %d", readGameState.currentLevel);
              // Assert
          }

          fclose(file); 
        }
   }

   // read fr is always 1, but "read currentLevel" reports 48 ?!?.

It seems like a pointer issue... but I can't figure out where I'm going wrong.

Anybody?
Thanks again.





-Carl
 
After obvious correction (must be if (fr == 1)) and adding \n in format strings these two functions work fine (VC++ 9, Windows XP sp2).
It's funny: you assign 2000 to u8 score member variable but max value of u8 type is 255.
It's impossible to save/restore any game state with these functions because of local state structures used.
So it's not the true save/restore code of your program.
In other words, these stubs work fine and it's impossible to reproduce your troubles in this simplified environment.
 
You're right, ArkM.
The 2000 was a slip of my fat fingers. Not sure why I didn't get a warning out of that in the IDE, but no matter.

Turns out the issue was in my toolchain. So short of anybody else loading up my entire devKit, it would've been impossible to reproduce.

I hate toolchains! lol

But thank you for all the effort. Sorry to have waisted your time.


-Carl
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top