I am trying to output a structure with 256 (0 - 255) records to a file. Then, load them when needed. I am telling fread to read to and write from one large structure, and I'm assuming that they will always be next to each other in memory. See how I did it below... Is this the correct way to do this, I mean, is it ok to assume that all the 256 structure records will be placed side by side, every time?
Code:
struct devDevice {
char Name[16];
unsigned char Code;
unsigned char Type;
unsigned char Zone;
unsigned char Status;
};
struct devDevice devX10[256];
Code:
void saveAll(void) {
FILE* WriteFile;
WriteFile=fopen("devices.dat", "wb");
if(WriteFile!=NULL) {
fwrite(&devX10[0x00], sizeof(struct devDevice), 256, WriteFile);
fclose(WriteFile);
} else {
LogIt("MAIN >> Device write error");
}
LogIt("MAIN >> Cfg file saved");
}
Code:
void readAll(void) {
FILE* ReadFile;
ReadFile=fopen("devices.dat", "rb");
if(ReadFile!=NULL) {
fread(&devX10[0x00], sizeof(struct devDevice), 256, ReadFile);
fclose(ReadFile);
} else {
LogIt("MAIN >> Device read error");
}
LogIt("MAIN >> Cfg file loaded");
}
Thanks,
Jon Scott