problem on writing to a binary file

This is a discussion on problem on writing to a binary file within the C Programming forums, part of the General Programming Boards category; i have a problem on how can i write a linked list structure to a binary file Code: typedef struct ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    20

    problem on writing to a binary file

    i have a problem on how can i write a linked list structure to a binary file

    Code:
    typedef struct pokemon{
            charn name[20];
            struct pokemon *next;
    }pokemon_t;
    i am opening a .bin file and get the structures and link it...
    the problem is how can i write it on the same .bin file?

    1. does "ab+" means appending on the last? so does it mean i can't write something on the start or on the middle of the file?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Are you trying to write to the same file you read from? If so, then you'll have to decide whether you want it to be at the end, on top of, or what.

    Trying to binarily write a linked list is not necessarily a good idea, since the pointers will not be valid at a future time so why bother writing them? Just write the data, and then when reading it in re-create the linked list.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    20
    because i'm adding a structure into the .bin file alphabetically...

    so do i need to link it? or there are other ways?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by eyescythe View Post
    because i'm adding a structure into the .bin file alphabetically...
    That would imply you would need to write the entire file out from scratch.

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    20
    if that so...i will write the newly alphabetically arranged structures to a new .bin file right?

    then...what if i need to load it? my program opens the old file...but ive written the alphabetically arranged structures on a new file...?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by eyescythe View Post
    if that so...i will write the newly alphabetically arranged structures to a new .bin file right?
    Why would you write it out to a new file?

    (You could write it out to a new file, and then rename it; but you might as well just write it in place.)

  7. #7
    Registered User
    Join Date
    Oct 2009
    Posts
    20
    so if i will just write it in place..

    how can i add a new structure in the file...alphabetically...so i nid to link it..
    or noy needed?
    Last edited by eyescythe; 10-17-2009 at 08:56 PM.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    It's not physically possible to write in the middle of a file. That's why you need to re-write the whole file from the beginning to the end.

  9. #9
    Registered User
    Join Date
    Oct 2009
    Posts
    20
    Quote Originally Posted by tabstop View Post
    It's not physically possible to write in the middle of a file. That's why you need to re-write the whole file from the beginning to the end.
    so if i will rewrite the file.. how can i do that?

    Code:
               i am opening the file like this
    
               bfp=fopen("myfile.bin", "ab+");
    will i truncate myfile.bin to zero? or what? how can i rewrite the file?

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Presumably you will open in r mode to read and w mode to write.

  11. #11
    Registered User
    Join Date
    Oct 2009
    Posts
    20
    Quote Originally Posted by tabstop View Post
    Presumably you will open in r mode to read and w mode to write.
    so what i will do now...is open the file...read the structures...link it...then close the file....add the new structure (in alphabetical)

    open the file again...with "wb+"....then write the link lists?

    so is that right?

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Seems like it should work.

  13. #13
    Registered User
    Join Date
    Oct 2009
    Posts
    20
    thanks tabstop....

    how about in fwriting the linked lists
    will i do this?

    head is the head pointer of the linked lists..

    fwrite(&head, sizeof(pokemon_t), 1, bfp) or
    fwrite(&pokemon, sizeof(pokemon_t), 1, bfp)?

    or is it right?
    Last edited by eyescythe; 10-17-2009 at 09:50 PM.

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Neither. If head is the pointer to the first element, and the first argument of fwrite is the pointer to the data, then I'm guessing you're already there without any decoration.

    You probably shouldn't be using head though, since you'll need to walk the list so you want to use a pointer that you can move through the list.

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    That's the "straight up" way to do it. Here's two more ways it can be done:

    1) Your current file records are already sorted, so just write out your new record, at the end of the file.

    But then it will be out of order!, I hear you say. You don't care.

    Because you're going to build a small integer array, and have it used as an index to the right sorted order of your records. If you had 5 records, (links), your index after you added one new unsorted record or link, would look like this:

    Index numbers:
    0, 1, 2, 3, 4, 5
    ==========
    Have values of record numbered:
    1, 2, 4, 5, 6, 3, because the new record would sort into the third place, if it were in order.

    Now to print up your data, you do this:

    for(i = 0; i < MaxRecords; ++i)
    printf(" Record #%d: whatever kind of data is in your link", i, index[i] recordNumber);

    Indexing is a VERY powerful thing to use for any data storage program. The index file would be a small text file, in that directory. When your program started, you'd have it load the data, and build the index array.

    2) Use fixed length Records. If you know that every record has exactly 120 bytes, then you can overwrite any record right in the middle of the data file, just by repositioning the file pointer, before you start writing data.

    Don't do this with sensitive data, because it's easy to goof up at first. Once you have it down, you'll see it's quite reliable though. You'll probably also see that the #1 way, is the way you want to go. Simple, powerful, quick. You only load the index array once, at the start of the program. If your data has fixed field records, then you can "jump" right to the right record, straight from the index data, using the file positioning techniques I just mentioned.

    These may be more than you want or need, or feel up to taking on just now, but they are available, when you're ready.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems installing Mingw5.1.4
    By BlackOps in forum C Programming
    Replies: 2
    Last Post: 07-26-2009, 03:28 AM
  2. 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
  3. Problem in writing string to File
    By Bargi in forum C Programming
    Replies: 4
    Last Post: 02-18-2009, 11:05 AM
  4. Problem writing to file
    By HAssan in forum C Programming
    Replies: 7
    Last Post: 11-05-2005, 08:59 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

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