array of structures help!

This is a discussion on array of structures help! within the C Programming forums, part of the General Programming Boards category; I am writing a small program which contains an array of structures, when the program is run it will save ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    51

    array of structures help!

    I am writing a small program which contains an array of structures, when the program is run it will save the data from the array of structures onto disk in a binary file, this bin file will also be available for reading by the user. I know all this uses the fwrite and the fread functions, but I am having problems getting to grips with this. Could someone please give me a small example. Here is my array of structures:

    Code:
    #include<stdio.h>
    
    struct data{
    
    char product[25];
    int amount;
    float cal;
    
    };
    
    struct data plan[500];
    
    int main()
    {
    
    /*code....*/
    }

  2. #2
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    writing

    Code:
    struct data plan[500];
    FILE *fp = fopen("output.dat", "wb");
    fwrite(plan, 1, sizeof(struct data) * 500, fp);
    reading
    Code:
    struct data plan[500];
    FILE *fp = fopen("output.dat", "rb");
    fread (plan, 1, sizeof(struct data) * 500, fp);

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    51
    Thankyou very much Brian for your quick response, thats exactly what I wanted, I will check it out.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    51
    Also one more question, how would I set a value to an individual variable. for instance - set the value of product to "apples" in the 25th structure. (set values before runtime)

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,788
    Be aware that this method of writing/reading would not work if your struct contained any pointers to dynamically allocated memory since the write/read would only be writing/reading a memory address and not the data pointed to by the pointers. You're OK since you don't have any of that but I just wanted you to be aware of that in the future.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    51
    Thankyou hk_mp5pdw I will bear that in mind. can you tell me how to set a value as described above. Ive been trying the code below but to no avail.

    plan[1].product = "apples";

    Is this wrong all together or am I writing this in the wrong part of the code??

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    Code:
    strcpy(plan[1].product, "apples");
    Last edited by itsme86; 08-03-2005 at 02:30 PM.
    If you understand what you're doing, you're not learning anything.

  8. #8
    Registered User
    Join Date
    Aug 2005
    Posts
    51
    Thankyou itsme86. And thankyou to everyone else, a very productive team of members on this board.

  9. #9
    Registered User
    Join Date
    Aug 2005
    Posts
    51
    Code:
    #include<stdio.h>
    
    
    struct data{
    
    char product[25];
    int amount;
    float cal;
    
    };
    
    struct data plan[500];
    
    int main()
    {
    strcpy(plan[1].product, "apples");
    
    printf("testing"):
    
    
    }
    Still wont compile, even if i pass the value before main{
    any suggestions?

  10. #10
    Registered User
    Join Date
    Aug 2005
    Posts
    51
    OK Ive realised what I was doing wrong now, I had to include the windows.h header, now its fine. Thankyou.

  11. #11
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    Code:
    printf("testing"):
    You have a : instead of a ;
    If you understand what you're doing, you're not learning anything.

  12. #12
    Registered User
    Join Date
    Aug 2005
    Posts
    51
    Yes and also to change the : to ;. Thankyou itsme86.

  13. #13
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    You also have no gurantee of being able to read the structures back in if you compile the program on another compiler and try to read it back (or even on another version of the compiler).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-05-2006, 11:06 PM
  2. filling an array of structures?
    By voodoo3182 in forum C Programming
    Replies: 9
    Last Post: 08-06-2005, 05:29 PM
  3. Array of Structures: Sorting
    By Drainy in forum C Programming
    Replies: 3
    Last Post: 04-13-2005, 09:55 AM
  4. Filling an Array of Structures
    By Zildjian in forum C Programming
    Replies: 5
    Last Post: 11-12-2003, 04:54 PM
  5. Need serious help on array of structures
    By cwd in forum C Programming
    Replies: 2
    Last Post: 11-11-2001, 02:39 PM

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