Letalis
01-04-2001, 09:34 AM
With fwrite and a binary file youre supposed to be able to write a struct to a file easily right?
like this:
list is a pointer to a node in a linked list
fwrite(list,sizeof(reg_node_t),1,filepointer);
but it doesnt work. It writes the first string in the struct and nothing more. Why is that?
TheLinuxDuck
01-04-2001, 11:51 AM
Firstly, what is reg_node_t? Something tells me it's an actual variable, and not a structure of some type.. that is probably why.. if the size of reg_node_t isn't static, you'll never get the same size dump each time.
I created a simplistic struct dump/load program to show how fread and fwrite work.. here is the code.. http://www.linuxnewbie.org/ubb/smile.gif
// Demonstrate the usage of fread/fwrite to dump an entire block of
// structs down to a file, and reread it.
//
#include <stdio.h>
//
// Create a dummy struct to fill
//
struct appliance {
char name[40];
char make[40];
char model[40];
char year[5];
};
//
// Declare functions
//
void fillapp(const char *sname,const char *smake,const char *smodel,
const char *syear,struct appliance *this);
void displayapps(struct appliance *sapps);
short int saveapps(struct appliance *sapps);
short int loadapps(struct appliance *sapps);
//
// Main function
//
int main(void)
{
//
// myapps is used for creating the file, myotherapps is used for reading
// from the file to demonstrate that the contents were read correctly
//
struct appliance myapps[10];
struct appliance myotherapps[10];
//
// Fill in struct info with dummy info
//
fillapp("Refridgerator","GE","P-987","1987",&myapps[0]);
fillapp("Microwave","GE","K-987","1987",&myapps[1]);
fillapp("Toaster","HP","PO-x1","1999",&myapps[2]);
fillapp("Oven","GE","J-9888","1989",&myapps[3]);
fillapp("Can Opener","Tivda","Iiu-9","1987",&myapps[4]);
fillapp("Bean Sprouter","Kookoo","Y88","1789",&myapps[5]);
fillapp("Coffee Grinder","CGI","Xp-9.j","1998",&myapps[6]);
fillapp("Washer","Highpoint","Uup-9","1978",&myapps[7]);
fillapp("Flaffy Flaff","Flaff","Fl.aff","1989",&myapps[8]);
fillapp("Rotato","Fliffy","Fl.i9","1996",&myapps[9]);
//
// Display the contents
//
displayapps(myapps);
//
// If it was saved successfully, reload and display
//
if(saveapps(myapps)) {
printf("Apps saved. Reloading...\n");
if(loadapps(myotherapps)) {
printf("Load successful:\n");
displayapps(myotherapps);
}
}
return(0);
}
//
// Fillapp fills one slice of the array at a time
//
void fillapp(const char *sname,const char *smake,const char *smodel,
const char *syear,struct appliance *this)
{
strcpy(this->name,sname);
strcpy(this->make,smake);
strcpy(this->model,smodel);
strcpy(this->year,syear);
}
//
// displayapps uses a pointer to the main struct to display everything
//
void displayapps(struct appliance *sapps)
{
short int i;
for(i=0;i<10;++i) {
printf("Record #%d\tName: %s\tMake: %s\nModel: %s\tYear: %s\n",
i,sapps[i].name,sapps[i].make,sapps[i].model,sapps[i].year);
}
}
//
// saveapps dumps all data in the struct down to a file, using fwrite
//
short int saveapps(struct appliance *sapps)
{
FILE *fptr;
short int returnvalue;
fptr=fopen("apps.data","wb");
if(fptr==NULL) {
perror("saveapps: Couldn't create apps.data");
return 0;
}
returnvalue=fwrite(sapps,sizeof(struct appliance),10,fptr);
if(returnvalue!=10)
printf("saveapps: Fwrite returned a short count of %d items.\n",
returnvalue);
else
printf("saveapps: Fwrite successful.\n");
fclose(fptr);
return 1;
}
//
// loadapps reads the data from the saved file in one fell swoop with fread
//
short int loadapps(struct appliance *sapps)
{
FILE *fptr;
short int returnvalue;
fptr=fopen("apps.data","rb");
if(fptr==NULL) {
perror("loadapps: Couldn't open apps.data");
return 0;
}
returnvalue=fread(sapps,sizeof(struct appliance),10,fptr);
if(returnvalue!=10)
printf("loadapps: Fread returned a short count of %d items.\n",
returnvalue);
else
printf("loadapps: Fread successful.\n");
fclose(fptr);
return 1;
}
This may be more than you were looking for, but I wanted to make sure and provide a compilable example to give you what you were looking for. http://www.linuxnewbie.org/ubb/smile.gif
------------------
TheLinuxDuck
I have a belly button.
:wq
I really want to know what a flaffy flaff is http://www.linuxnewbie.org/ubb/wink.gif
Paul