Is there anything wrong with doing this?

This is a discussion on Is there anything wrong with doing this? within the A Brief History of Cprogramming.com forums, part of the Community Boards category; I am trying to output a structure with 256 (0 - 255) records to a file. Then, load them when ...

  1. #1
    Registered User jon_nc17's Avatar
    Join Date
    May 2002
    Posts
    15

    Post Is there anything wrong with doing this?

    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

  2. #2
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001
    one suggestion:

    void is EVIL!
    PHP and XML
    Let's talk about SAX

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 04:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 01:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 02:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21