Thread: Structures and Files

  1. #1
    Registered User
    Join Date
    Sep 2013
    Location
    Jamaica
    Posts
    134

    Structures and Files

    I was wonder if I can make 2 different size structures in a file. I wrote a code but It's apart of my school project and I havent compiled it yet, so Im just wondering will this work?

    Code:
    if((stsPtr = fopen("status.dat", "rb")) == NULL)
    {
        prinft("File could not be created/opened.\n");
        Error();
    }
    else
    {
        fseek(stsPtr, 1 * sizeof(struct avail) SEEK_SET);
            fread(&counts1, sizeof(struct avail), 1, stsPtr);
        for(x = 0; x < 50; x++)
        {
            availSSlot[x] = counts.slot[x];
        }
        int cStudNum = sCount1;
        int cStudNum1 = sCount2;
        int    cStudNum2 = sCount3;
        int cStudNum3 = sCount4;
        int cStudNum4 = sCount5;
        int cStudNum5 = sCount6;
        int cAccNum = aCount;
        fseek(stsPtr, 0 * sizeof(struct stats) SEEK_SET);
        fread(&counts, sizeof(struct stats), 1, stsPtr);
    }
    fclose(stsPtr);

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    So long as you can do the maths in your fseek calls, you can arrange any structs in any order you want in the file.

    avail avail avail avail stats stats stats stats
    is one way

    avail stats avail stats avail stats avail stats
    is another way
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with Files and Structures
    By PenguinTux in forum C++ Programming
    Replies: 11
    Last Post: 03-23-2011, 02:31 AM
  2. Structures and Files
    By nuddy in forum C Programming
    Replies: 2
    Last Post: 03-31-2010, 12:15 PM
  3. Multiple Source Files with Structures
    By dac in forum C++ Programming
    Replies: 7
    Last Post: 01-20-2007, 05:44 AM
  4. Random access files and structures
    By DLR in forum C Programming
    Replies: 8
    Last Post: 04-21-2006, 03:24 PM
  5. structures and files
    By eth0 in forum C++ Programming
    Replies: 9
    Last Post: 01-06-2004, 06:48 PM