writing arrays of structures

This is a discussion on writing arrays of structures within the C Programming forums, part of the General Programming Boards category; how do you write an array of structures to a file, i tried this fwrite(&seats[x], sizeof seats[x], 1, fl); where ...

  1. #1
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501

    writing arrays of structures

    how do you write an array of structures to a file, i tried this
    fwrite(&seats[x], sizeof seats[x], 1, fl);
    where fl is the file handler and seats is an array of structures.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    fwrite( array, sizeof( struct structtype ), array_members, file );

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User Twiggy's Avatar
    Join Date
    Oct 2001
    Posts
    43
    This is how I would do it.

    Set a max for your array.

    Code:
    #define MAX_SEATS 10
    
    for (i = 0, i < MAX_SEATS, i++)
    {
        fprintf(fl, "%s", seats[i]);
    }
    I apologize if thats wrong. I'm learning C myself. However thats how i'd go about writing an array to a file.
    To error is human, to really foul things up requires a computer

  4. #4
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    thanx, but I am still getting seg faults when trying to write the structure to the hdd. here is what the structure looks like

    struct nm {
    char first[20];
    char last[20];
    };
    struct layout {
    short assign;
    struct nm name;
    } seats[12];

    I tried this

    fwrite(&seats, sizeof(struct layout), 12, fl);
    where fl is the file handler, can someone help?
    Last edited by chrismiceli; 06-26-2003 at 11:19 AM.

  5. #5
    Registered User Sargnagel's Avatar
    Join Date
    Aug 2002
    Posts
    166
    fwrite(seats, sizeof(struct layout), 12, fl);

    The above should work. You were using "&seats" and thereby passing the address of the address of seats to fwrite(), if I am correct.

    [edit]
    Ah, nevermind. "&seats" is correct.
    Some kind of "Alzheimer jr." attack.
    [/edit]
    Last edited by Sargnagel; 06-26-2003 at 11:58 AM.

  6. #6
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    oh, i forgot to assign the fl to the return of fopen, i did this
    if((fopen("/home/bane/chris.dat", "wb"))==NULL) {
    printf("Can't write ~/chris.dat!");
    exit(0);
    }

    sorry to waste your time,

  7. #7
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    See, always post enough code to let us know what's happening -- and don't forget code tags
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. more help...2d arrays in structures - sscanf
    By mapunk in forum C Programming
    Replies: 6
    Last Post: 12-01-2005, 06:00 PM
  2. Arrays Data Structures
    By silicon in forum C++ Programming
    Replies: 8
    Last Post: 09-19-2003, 01:40 PM
  3. writing linked structures
    By chrismiceli in forum C Programming
    Replies: 12
    Last Post: 07-06-2003, 01:14 AM
  4. Arrays in structures
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 02-12-2002, 10:54 AM
  5. Methods for Sorting Structures by Element...
    By Sebastiani in forum C Programming
    Replies: 9
    Last Post: 09-14-2001, 12:59 PM

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