Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > concatenating binary files

Reply
Thread Tools

concatenating binary files

 
 
Igor R.
Guest
Posts: n/a
 
      01-15-2009
What's the laconic and standard way to do the subj - without an
explicit loop of [allocate-read-write]? In case of text files, I'd do
something like:
ifstream in("file1.txt", ios::in);
ofstream out("file2.txt", ios:ut | ios::ate);
out << in.rdbuf();
But in case of binary files, I guess "<<" would do some undesired
formatting.
 
Reply With Quote
 
 
 
 
Ron AF Greve
Guest
Posts: n/a
 
      01-15-2009
Hi,


"Igor R." <> wrote in message
news:c1c01c6b-46d8-4150-8e34-...
> What's the laconic and standard way to do the subj - without an
> explicit loop of [allocate-read-write]? In case of text files, I'd do
> something like:
> ifstream in("file1.txt", ios::in);
> ofstream out("file2.txt", ios:ut | ios::ate);
> out << in.rdbuf();
> But in case of binary files, I guess "<<" would do some undesired
> formatting.


Open the files with ios_base::binary
and then use

char Buffer[ SomeSize ];

in.read( Buffer, sizeof( Buffer ) );
out.write( Buffer, in.gcount() );

In a loop.

Regards, Ron AF Greve

http://informationsuperhighway.eu/


 
Reply With Quote
 
 
 
 
sean_in_raleigh@yahoo.com
Guest
Posts: n/a
 
      01-15-2009
On Jan 15, 9:19 am, "Igor R." <igor.rubi...@gmail.com> wrote:
> What's the laconic and standard way to do the subj - without an
> explicit loop of [allocate-read-write]?



If you're really committed to doing it without a
loop, then something like this will do it:

#include <algorithm>
#include <fstream>
#include <iostream>
#include <iterator>

using namespace std;

void
append_file_to_stream(ofstream& out, const char *infile)
{
ifstream in(infile, ios_base::in | ios_base::binary);
in.exceptions(ios_base::badbit | ios_base::failbit);

istreambuf_iterator<char> in_it(in);
istreambuf_iterator<char> end;
ostreambuf_iterator<char> out_it(out);

copy(in_it, end, out_it);
}

int main()
{
ofstream out("outfile.bin", ios_base:ut | ios_base::binary);
out.exceptions(ios_base::badbit | ios_base::failbit);

append_file_to_stream(out, "infile1.bin");
append_file_to_stream(out, "infile2.bin");
}


It's got a certain charm, but I've never really used this
kind of thing in real code, so YMMV.

Sean

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Concatenating selective lines from a bunch of files into a singlefile Rider Perl Misc 6 07-26-2009 04:04 PM
Efficiently concatenating contents of multiple files sasuke Java 5 07-06-2008 02:27 AM
Newbie: working with binary files/extract png from a binary file Jim Ruby 5 02-01-2008 11:21 AM
Concatenating Audio Files in with Perl on a Mac Gary Morrison Perl 5 03-28-2006 12:45 AM
Continuously concatenating binary data Unforgiven C++ 5 10-16-2003 08:37 PM



Advertisments