Thread: writing to binary

  1. #1
    Wanabe Laser Engineer chico1st's Avatar
    Join Date
    Jul 2007
    Posts
    168

    writing to binary

    I have 2 billion integers i would like to write to a binary file (long story). If i do such a thing would i need to write it in as a giant object?

    how would this go down?

    Im out to find a C book that can help me now, if i cant find one at my library i was hoping you guys could save me.

    Thanks!

  2. #2

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Do you want to write the numbers 0..2billion in sequence, or is there something generating numbers (e.g. random numbers, data-values from a measurement, or some such) that you need to write?

    It shouldn't be too hard to come up with about 5 lines that do what you want, but it's necessary to understant what you are actually trying to do first.

    Also, you need to check that the OS and C-library supports large files (up to about 8GB). Linux in any modern form will be fine, Windows NT/2K/XP/Vista should also be fine.

    And of course, you'll need some time for the data to transfer - at least a minute and a half just in transfer-time from the memory to the disk, and the disk may need extra time to move the head around and such, so count on at the very least two minutes.

    --
    Mats

  4. #4
    Wanabe Laser Engineer chico1st's Avatar
    Join Date
    Jul 2007
    Posts
    168
    yeah there is something generating the numbers and I have them in a giant array, then i want to write them sequentially to 1 binary file.

    I was mostly concerned that writing individual numbers to a binary file wouldn't work out the way i planned.

    and im using windows xp so i guess ill be ok.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Why wouldn't it work - binary files can contain anything. Just make sure you specify "wb" as the mode to fopen when you open the file, so that the C-library understands that it shouldn't try to "fix it up" when it sees a newline ['\n'} (which in text-mode should have a '\r' prepended).

    You can also write a bunch of data at once, but I wouldn't attempt to write the whole array at once, perhaps a few thousand numbers at a time.

    --
    Mats

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    If you're not interested in the issues of Endianess or the potential different sizes of an int on different architectures, because this is a test program meant for one platform to learn something, then you should be fine. Go for it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing std::string in binary mode
    By VirtualAce in forum C++ Programming
    Replies: 16
    Last Post: 11-06-2010, 07:39 PM
  2. Writing a struct to a binary file
    By Scarvenger in forum C++ Programming
    Replies: 8
    Last Post: 09-12-2006, 01:50 AM
  3. Writing binary data to a file (bits).
    By OOPboredom in forum C Programming
    Replies: 2
    Last Post: 04-05-2004, 03:53 PM
  4. binary search and search using binary tree
    By Micko in forum C++ Programming
    Replies: 9
    Last Post: 03-18-2004, 10:18 AM
  5. binary to decimal
    By miryellis in forum C Programming
    Replies: 7
    Last Post: 03-14-2004, 08:35 PM