C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 01-20-2008, 11:23 PM   #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.
chinook86 is offline   Reply With Quote
Old 01-20-2008, 11:31 PM   #2
Registered User
 
Join Date: Sep 2004
Location: California
Posts: 2,845
Quote:
I know I could just read in and write
That's the right way.
bithub is offline   Reply With Quote
Old 01-20-2008, 11:31 PM   #3
CSharpener
 
vart's Avatar
 
Join Date: Oct 2006
Posts: 5,242
probably cat works faster because it reads/writes in big chunks
__________________
If I have eight hours for cutting wood, I spend six sharpening my axe.
vart is offline   Reply With Quote
Old 01-21-2008, 12:12 AM   #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.
invinciblevn is offline   Reply With Quote
Old 01-21-2008, 12:27 AM   #5
Woof, woof!
 
zacs7's Avatar
 
Join Date: Mar 2007
Location: Australia
Posts: 3,139
No, fgets() and fputs() aren't binary-safe.

You mean, fread() and fwrite()
__________________
"I.T. gets the chicky-babes" - M. Kelly
bakefile | vim
zacs7 is offline   Reply With Quote
Old 01-21-2008, 03:18 AM   #6
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
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.
matsp is offline   Reply With Quote
Old 01-21-2008, 05:26 AM   #7
and the hat of vanishing
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,214
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.
Up to 8Mb PlusNet broadband from only £5.99 a month!
Salem is offline   Reply With Quote
Old 01-21-2008, 02:19 PM   #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.
chinook86 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 09:09 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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