Jump to content
  • Advertisement
Sign in to follow this  
Q-Parser

Need help with loading struct from a binary in C

This topic is 4945 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello there! I'd need help with loading an array of structs from a binary. I saved 2 items of array to a file and now I want to read them back. My code does not work. It outputs nothing...
#include <stdio.h>
#include <string.h>

typedef enum {male, female} SEX;

typedef struct persons {
	char name[10];
	char surname[15];
	SEX sex;
	int age;
} PERSONS;


int main() {
  PERSONS *friend;
  FILE *f;
  unsigned int filesize, i;
  
  friend=(PERSONS*)malloc(sizeof(PERSONS)*10); //array of structs

  if ((f=fopen("workfile","rb"))==NULL) {  //open file for binary read
	printf("Could not open file for reading!\n");
	exit(1);
	}
  
  fseek(f, 0, SEEK_END); //cursor at the end of file
  filesize=ftell(f); //size of file in bytes
  
  fread(friend, sizeof(PERSONS), filesize/sizeof(PERSONS),f);

  //this is just to check how many items of array are stored in the file
  printf("Size of PERSONS structure: %d\n", sizeof(PERSONS)); //outputs 36
  printf("Current position of cursor in the file is: %d\n", filesize);  //outputs 72
  printf("Number of items in the file: %d\n", filesize/sizeof(PERSONS)); //outputs 2
  
  for(i=0; i<10; i++) {              //outputs nothing, blank and zeros for integers, except for id numbering, of course!
	printf("id: %d\n", i);
	printf("Name: %s\n", friend.name);
	printf("Surname: %s\n", friend.surname);
	printf("Sex: %d\n", friend.sex);
	printf("Age: %d\n\n", friend.age);
        }	
  
  if (fclose(f)==EOF) {     //close file
	printf("Could not close file!\n");
	exit(1);
        }
  free((void*)friend);   //free pointer
  friend=NULL;
  
	
  return 0;
}



I don't get what's wrong with my code. Suggestions welcomed. Thanks

Share this post


Link to post
Share on other sites
Advertisement
Your code looks fine to me (except I'm not entirely comfortable with reading/writing enums like that, but that's a seperate issue). Are you totally sure that your file is not full of zeros? Check it in a hex editor - Visual Studio will do it (if you're using Visual Studio that is), just rename the file to something like "binaryfile.bin" and open it in VS, and VS will work as a hex editor.

Share this post


Link to post
Share on other sites
Ad 1: No, my file is neither empty, nor full of zeros. It contains data I stored there. Text can be viewed, but there are no numbers to be seen in the file.
Ad 2: I'm running on linux:) But this is not the case.

Share this post


Link to post
Share on other sites
you fseek'd to the end before fread'ing. It goes EOF and does nothing, and you're left with the zero filled empty memory returned from malloc.

Share this post


Link to post
Share on other sites
Quote:
Original post by C-Junkie
you fseek'd to the end before fread'ing. It goes EOF and does nothing, and you're left with the zero filled empty memory returned from malloc.


Of couse! Damn, how stupid I am. I haven't noticed that. It works now.
Thank you very much!

Share this post


Link to post
Share on other sites
Quote:
Original post by j0seph
Quote:
Original post by C-Junkie
you fseek'd to the end before fread'ing. It goes EOF and does nothing, and you're left with the zero filled empty memory returned from malloc.


Of couse! Damn, how stupid I am. I haven't noticed that. It works now.
Thank you very much!
Don't feel stupid, everyone does the same thing occasionally. Things like this show how truly useful peer review is!
I spotted the same thing in about 10 seconds. No doubt other's could do the same when I blunder...

Share this post


Link to post
Share on other sites
Quote:
Original post by iMalc
Quote:
Original post by j0seph
Quote:
Original post by C-Junkie
you fseek'd to the end before fread'ing. It goes EOF and does nothing, and you're left with the zero filled empty memory returned from malloc.


Of couse! Damn, how stupid I am. I haven't noticed that. It works now.
Thank you very much!
Don't feel stupid, everyone does the same thing occasionally. Things like this show how truly useful peer review is!
I spotted the same thing in about 10 seconds. No doubt other's could do the same when I blunder...


Yes, everybody does mistakes from time to time. I hate it, after I've done a piece of code, that I forget about tiny things, which may often cause great troubles.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!