Thread: Merge Binary Files

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    12

    Merge Binary Files

    I have a program that creates multiple binary files. The problem is that I need to merge the files together. I can use the cat command afterwards and merge the files by hand. I would like however to merge the files together in the C program. I know I could just read in and write, but that sounds wasteful as cat seems to work much faster. Any help as usual appreciated.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    I know I could just read in and write
    That's the right way.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    probably cat works faster because it reads/writes in big chunks
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    18
    I think fgets() and fputs() is good for this case.
    Last edited by invinciblevn; 01-21-2008 at 12:22 AM.

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    No, fgets() and fputs() aren't binary-safe.

    You mean, fread() and fwrite()

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    cat _DOES_ fread/fwrite (or some variant thereof) - so, yes, it doesn't make much sense to write your own version of the same functionality.

    If you still want to do that, use large buffers for the read/write data - 4KB or bigger will be a good start.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  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
    A buffer size of BUFSIZ is meant to have pretty good performance characteristics (on average).

    cat may use the lower level read/write primitives which are a bit closer to the machine than the truly portable fread/fwrite. But you limit yourself to POSIX systems in doing so.

    You also have to weigh the efficiency of cat with the cost of spawning another process.
    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
    Apr 2007
    Posts
    12
    Thanks for the help guys. This was a programing assignment designed to learn system calls on a unix based system. Therefore, I decided to use the system("cat -u ....") command.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  2. Copying Binary Files
    By mikeman118 in forum C++ Programming
    Replies: 9
    Last Post: 08-11-2007, 10:55 PM
  3. Help with binary files
    By tuxinator in forum C Programming
    Replies: 3
    Last Post: 12-01-2005, 02:11 AM
  4. send/recv binary files using sockets in C++
    By dafatdude in forum Networking/Device Communication
    Replies: 14
    Last Post: 07-25-2004, 11:00 AM
  5. Binary files
    By Brian in forum C Programming
    Replies: 2
    Last Post: 02-18-2002, 01:13 PM