Thread: Bubblesort with unknown size struct read from a bin file

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    12

    Bubblesort with unknown size struct read from a bin file

    Hello folks, can you help me please?

    I have a binary file, and I want to pass these infos to a TXT ordered file.

    I can pass the data without problems if I don' try to order: I created a vector of struct of size 2, and I go reading the bin file, if my count of lines, get to the number 2, I write the data on the txt, then I put my count as zero again, and using this way, I can make it work without actually knowing how many lines my bin file has...

    Well, the problem is: by this method, I'm constantly deleting my structs, so how can I order the data?

    PS: I dont know how to allocate vector dinamically, so for now I want to understand how could I use a bubble sort to order these data from my bin file and then pass to a txt file, if I dont know how long this file is.

    Thanks in advance,
    Last edited by frank1; 10-03-2012 at 09:44 PM.

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Do you know the maximum number of records in your binary file?
    How about defining your array to have that many elements.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    12
    Hey oogabooga...

    No, I don't know the maximium number of records, if I knew I could make a for and solve my problem :/

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Use malloc to allocate the vector dynamically. Examples abound on the internet, but you might have more luck searching for "dynamic array C" (vecotr is perhaps more of a C++ term). If you feel like getting fancy, you can malloc some memory, then use realloc to expand it if you run out before you're done reading the file.

    Also, you could use some fseek and ftell "trickery" (it's not that tricky or tough) to figure out the size of the file, and divide that by the size of the struct. That may not be 100% accurate, since binary files may have some extra "junk" data at the end, and I can't remember how ftell works off the top of my head. I'll check and get back to you.

    EDIT: I was right, the fseek bit isn't guaranteed to work if you try to fseek using SEEK_END*. A slower, but guaranteed alternative is to read through the binary file one struct at a time, storing the data in a "dummy" variable, and counting as you go. Then malloc enough space, rewind the file to the beginning, and read it all in to your newly allocated array.

    * The relevant part of the standard if anybody cares is:
    Quote Originally Posted by C99 7.19.9.2p3
    ...A binary stream need not meaningfully support fseek calls with a whence value of SEEK_END.
    Last edited by anduril462; 10-03-2012 at 11:36 PM.

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    12
    Hello anduril462, like I said, I don't know and I can't use malloc...

    thanks for answering anyway

  6. #6
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Why can't you use malloc?

    It seems you have to use the brute force method: read the whole file once to determine the number of records, then define the needed array and read the records a second time, now storing them in the array.

    Bye, Andreas

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you can't malloc what you need, and you can't afford to allocate stupidly large arrays to cover "all" eventualities, then you need something like this
    External sorting - Wikipedia, the free encyclopedia
    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.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You are able to pass the records through to the txt file, OK, right?

    So instead of sorting it yourself in an array, (since you can't malloc yet), then let the system sort the text file, for you. In Windows type sort /?, in Linux type man sort, for all the details.

    And use the system() function, along with the sort options you want.

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by frank1 View Post
    I dont know how to allocate vector dinamically, so for now I want to understand how could I use a bubble sort to order these data from my bin file and then pass to a txt file, if I dont know how long this file is.
    I will note that this statement does not mean you CAN'T use malloc(). It means you don't know how.

    I suggest some effort in learning about malloc() will be more useful than trying to find an alternative solution. Yes, there are alternative solutions, but they are even more advanced (i.e. difficult to use for your scenario) than malloc().
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read in .dat file unknown number of rows
    By Chris Bromley in forum C Programming
    Replies: 3
    Last Post: 03-16-2012, 05:54 AM
  2. Read text file with unknown number of rows in C
    By Raymond2010 in forum C Programming
    Replies: 3
    Last Post: 06-25-2011, 11:27 PM
  3. read unknown integers from text file
    By underlink in forum C Programming
    Replies: 2
    Last Post: 11-10-2009, 10:23 AM
  4. Read from file of unknown length, then terminate
    By hello234 in forum C Programming
    Replies: 2
    Last Post: 10-12-2008, 04:33 PM
  5. fread struct with unknown size
    By ReggieBE in forum C Programming
    Replies: 12
    Last Post: 03-27-2006, 12:19 PM