Merge Binary Files

This is a discussion on Merge Binary Files within the C Programming forums, part of the General Programming Boards category; I have a program that creates multiple binary files. The problem is that I need to merge the files together. ...

  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,246
    I know I could just read in and write
    That's the right way.

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    probably cat works faster because it reads/writes in big chunks
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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-20-2008 at 11:22 PM.

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,507
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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, 01: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, 12:13 PM

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