Hi everybody,
I want to write an array of struct to a file as I want that information to be loaded next time I run my program but I seem to have trouble writing an array of struct to a file... I can see that it is writing something to file but it is all jibberish... and I'm not sure why either.. here is my relevant code..
my struct:
Code:
struct x{
char name[100];
int node;
short type;
};
declaring my struct:
Code:
struct x HOME[1]; // Used to be 10 but set to 1 to see if I can write at least one element of an array
function that writes to disk:
Code:
int closeDisk(struct x *d)
{
if (fseek(my_disk, 0, SEEK_SET) != 0)
{
printf("Disk access error!\n");
return 0;
}
fwrite((struct x*)d, sizeof(struct x), 1, my_disk);
return 1;
}
when program starts up again, inside function that reads from file:
Code:
if ((my_disk = fopen(filename, "wb")) == NULL) // Create or open a new file
{
printf("Error in opening the disk simulation file\n");
exit(1);
}
else
{
if (fseek(my_disk, 0, SEEK_SET) != 0)
{
printf("Disk access error!\n");
return 0;
}
fread((struct x*)d, sizeof(struct x), 1, my_disk);
// Test print to see what is read into struct
printf("%s\n%d\n",x[0].name,x[0].node);
}
Does anyone see anything wrong? Any help would be greatly apprectiated...
Thanks.