Thread: structures

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    109

    structures

    I can't understand this code
    Code:
    #include <stdio.h>
    
    /* random record description - could be anything */
    struct rec
    {
        int x,y,z;
    };
    
    /* writes and then reads 10 arbitrary records
       from the file "junk". */
    int main()
    {
        int i,j;
        FILE *f;
        struct rec r;
    
        /* create the file of 10 records */
        f=fopen("junk","w");
        if (!f)
            return 1;
        for (i=1;i<=10; i++)
        {
            r.x=i;
            fwrite(&r,sizeof(struct rec),1,f);
        }
        fclose(f);
    
        /* read the 10 records */
        f=fopen("junk","r");
        if (!f)
            return 1;
        for (i=1;i<=10; i++)
        {
            fread(&r,sizeof(struct rec),1,f);
            printf("%d\n",r.x);
        }
        fclose(f);
         
        return 0;
    }
    The bit I'm confused about is the bit where the program writes a number to the structure then to the file

    Code:
    for (i=1;i<=10; i++)
        {
            r.x=i;
            fwrite(&r,sizeof(struct rec),1,f);
        }
    doesn't this mean r.x = the value of i, but once it loops around would r.x equal the new value of i and wipe the old value out of its memory?

  2. #2
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    doesn't this mean r.x = the value of i, but once it loops around would r.x equal the new value of i and wipe the old value out of its memory?
    Yes that's exactly right. The old value gets overwritten. But that's okay, because you have already written that previous value to the file and thus have no need to store the old value.
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  3. #3
    Registered User
    Join Date
    Aug 2002
    Posts
    109

    ummm

    ahhh I think i get it, when you save r.x to a file it dosn't overwirte any previous r.x's but in the program it will.

    Please tell me if you think I am worng!

  4. #4
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    In your program memory you declared:
    struct rec r;

    You only have one copy of this and will overwrite the old values every loop. But before you overwrite it you called fwrite(). This wrote your value to the file and incremented your file pointer f to the next position of the file. So you won't overwrite any values in the file unless you have moved your file pointer backwards such as using the fseek command.
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-05-2006, 11:06 PM
  3. Input output with structures
    By barim in forum C Programming
    Replies: 10
    Last Post: 04-27-2004, 08:00 PM
  4. pointers to arrays of structures
    By terryrmcgowan in forum C Programming
    Replies: 1
    Last Post: 06-25-2003, 09:04 AM
  5. Methods for Sorting Structures by Element...
    By Sebastiani in forum C Programming
    Replies: 9
    Last Post: 09-14-2001, 12:59 PM