Thread: Loading structure from file

  1. #1
    Software engineer
    Join Date
    Aug 2005
    Location
    Oregon
    Posts
    283

    Loading structure from file

    This code saves an actor to a file. After modifying the age, I load the actor back from file. What's strange is it still has the modified age. I would expect it to be 28 again? Edit: The loading function returns 1.

    Code:
    #include <stdio.h>
    #include <stdlib.h>   
    #include <string.h> 
    
    typedef struct _skills
    {
    	int short_sword;
    	int axe;
    } SKILLS;
    
    typedef struct _actor
    {
    	char name[20];
    	int age;
    	SKILLS skills;
    } ACTOR;
    
    void Actor_Init(ACTOR *p_Actor, const char *name, int age)
    {
    	strcpy(p_Actor->name, name);
    	p_Actor->age = age;
    	p_Actor->skills.short_sword = 50;
    	p_Actor->skills.axe = 5;
    }
    
    int Actor_Save(ACTOR *p_Actor, const char *filename)
    {
    	FILE *fp;
    
    	fp = fopen(filename, "wb");
    	if (fp == NULL)
    		return 0;
    
    	fwrite(&p_Actor, sizeof(ACTOR), 1, fp);
    
    	fclose(fp);
    
    	return 1;
    }
    
    int Actor_Load(ACTOR *p_Actor)
    {
    	FILE *fp;
    
    	fp = fopen("actors.dat", "rb");
    	if (fp == NULL)
    		return 0;
    
    	fread(&p_Actor, sizeof(ACTOR), 1, fp);
    
    	fclose(fp);
    
    	return 1;
    }
    
    void Actor_Show(ACTOR *p_Actor)
    {
    	printf("Name: %s\n", p_Actor->name);
    	printf("Age: %d\n", p_Actor->age);
    	printf("Sword: %d\n", p_Actor->skills.short_sword);
    	printf("Axe: %d\n", p_Actor->skills.axe);
    }
    
    int main(void)
    {       
    	ACTOR PC;
    
    	Actor_Init(&PC, "Test", 28); 
    	Actor_Save(&PC, "actors.dat");
    	
    	PC.age = 29;
    
    	Actor_Load(&PC);
    
    	Actor_Show(&PC);
    	 
    	return 0;
    }
    Last edited by dxfoo; 07-13-2010 at 06:45 PM.

  2. #2
    Software engineer
    Join Date
    Aug 2005
    Location
    Oregon
    Posts
    283
    Oh, I got it. I don't get it though.

    I changed
    fread(&p_Actor, sizeof(ACTOR), 1, fp);

    to
    fread(p_Actor, sizeof(ACTOR), 1, fp);

    The PC object is passed to Actor_Load() as a reference. Then I try loading it as a reference again. But it already is a reference?

  3. #3
    Software engineer
    Join Date
    Aug 2005
    Location
    Oregon
    Posts
    283
    I get it now. I'm passing an address to the function. The formal parameter contains the address. So I don't have to specify '&' again. If you do it can do bad things!

  4. #4
    Registered User
    Join Date
    Jul 2010
    Posts
    55
    I feel your pain, pointers sure give me headaches sometimes, it's probably cuz I haven't used them much though

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  3. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  4. need help with .md3 file loading
    By Shadow12345 in forum Game Programming
    Replies: 2
    Last Post: 12-06-2002, 04:06 PM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM