PDA

View Full Version : Is there anything wrong with doing this?



jon_nc17
11-20-2002, 09:18 PM
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?



struct devDevice {
char Name[16];
unsigned char Code;
unsigned char Type;
unsigned char Zone;
unsigned char Status;
};

struct devDevice devX10[256];






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");
}





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

Waldo2k2
11-21-2002, 11:30 AM
one suggestion:

void is EVIL!