fread() and fwrite() ?

This is a discussion on fread() and fwrite() ? within the C Programming forums, part of the General Programming Boards category; Hi, I have a quick general question. For reading and writing in binary mode, I have learned that there are ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    4

    Question fread() and fwrite() ?

    Hi,

    I have a quick general question. For reading and writing in binary mode, I have learned that there are functions such as fread() and fwrite(). fread() takes a pointer to a location in memory where adequate space has been allocated, the size of a single unit of the type of data you are going to be reading in to that location from a file, the number of those individual types from the previous parameter, and finally, the stream that you are reading from. I have learned that it basically copies the data from the file to the location pointed to by the first parameter... fwrite() works in a similar manner, but basically copies the number of data items starting with the memory location pointed to by the first parameter. I have noticed that both functions work sequentially, and basically seem to use a starting point in memory and the number of items of the specified size. What about data structures that are not put together like arrays. It seems that the way these functions work, they are good only for data structures like arrays that are lied out one after another in memory. A linked list for example is not linked due to the way it is laid out. In memory, it could be all over the place, but it is intact because each node contains a pointer to the next structure. In an instance like this, simply providing a pointer to the first node of the linked list does not guarantee that directly after that node in memory, node #2 will follow. Are there any functions built into C that can handle writing structures that are not sequentially laid out in memory to a binary file? Any suggestions would be much appreciated.

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Nope, sorry.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    4
    Thanks QuestionC

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    > Are there any functions built into C that can handle writing structures that are not sequentially laid out in memory
    No.

    But writing out a linked list is not hard. The only thing you've got to remember is that you cannot write out pointers to a file (well you can, they just don't mean anything when you read them back).

    Code:
    // separating the data from the LL pointer makes for easy fread/fwrite use
    typedef struct {
        int data;
        char name[10];
    } data_st;
    typedef struct _node {
        data_st     data;
        struct _node *next;
    } node_st;
    
    void write_list ( node_st *head, FILE *fp ) {
        while ( head ) {
            fwrite( &head->data, sizeof(data_st), 1, fp );
            head = head->next;
        }
    }
    Reading back in is only slightly more tricky - you have to allocate a new node for each fread, then append it to the list.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    4

    Thumbs up

    I get it... basically you end up with a file that contains nodes without links written in the order that they were originally linked. Because they will most likely be read back to different memory addresses, the original addresses that were stored in the pointers are useless. So you write the list as a bunch of nodes without any links other than the order in which they were written (i.e. head = head->next) and then when you read them back you can reestablish new pointers. Thanks alot Salem. : )

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2d array and fwrite and fread
    By totalnewbie in forum C Programming
    Replies: 14
    Last Post: 01-10-2009, 02:45 PM
  2. Replies: 2
    Last Post: 12-26-2008, 03:38 PM
  3. fwrite / fread question
    By Takteek in forum C Programming
    Replies: 5
    Last Post: 11-27-2008, 03:18 AM
  4. Weird problem with fwrite() and fread()
    By piote in forum C Programming
    Replies: 2
    Last Post: 11-13-2004, 02:07 PM
  5. buffer type for fread & fwrite
    By daluu in forum C Programming
    Replies: 5
    Last Post: 05-08-2003, 06:57 PM

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