reading structures from a file

This is a discussion on reading structures from a file within the C Programming forums, part of the General Programming Boards category; I want to be able to create a structure record (person name, id number, rating) and save it to a ...

  1. #1
    bishopcl
    Guest

    reading structures from a file

    I want to be able to create a structure record (person name, id number, rating) and save it to a file. I then want to be able to pull the person based on ID number. How? I have tried using the fwrite and fread commands, but I can only "write" one structure at a time. And upon reading it back, I can't seem to pull individual data based on a certain criteria. I'm new at programming, what is it I'm missing about writing and reading info to files??

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    fread/fwrite(void * data, unsigned packets, size_t size_of_packet, FILE * file)

    So ten structs at once would be :

    struct Struct array[10];
    int size = sizeof(Struct);

    int success == fread(array, 10, size, file);

    or even:

    success == fread(array, 1, size * 10, file);

    or even:

    success == fread(array, size * 10, 1, file);

    or:

    while(size == fread(array, 1, size, file))
    continue;

    So pretty flexible to use these functions. Just read the return value and if is not the same number you passed as the second parameter to fread/fwrite, then an error occured such as end of file, unopened or invalid FILE handles, etc...
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  3. #3
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>So pretty flexible to use these functions
    Unless the structure uses pointers to anonymous memory, such as a name. If this is the case then onlny the value of the pointer will be saved, not the memory it points to. So if your structure looks like this
    Code:
    typedef struct {
      char *name;
      long id_num;
      long rating;
    } PERSON;
    Then using fread won't work to write any one of the structures to file. You'd have to write each member of each structure manually and read them back manually :-)
    *Cela*

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    Like hard-coded buffers of known size.

    unsigned id;
    char name_buffer[128];
    char address_buffer[128];
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

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