Question

I have two binary files and I would like to append one with the other. How can I do it?

std::ofstream outFile;
outFile.open( "file.bin", ? );

what should be the nest line?

No correct solution

OTHER TIPS

There's a one liner for this:

std::ofstream outFile("file.out", std::ios::ate );
std::ifstream inFile( "file.in" );

std::copy( 
    (std::istreambuf_iterator<char>(inFile)),  // (*)
     std::istreambuf_iterator<char>(),
     std::ostreambuf_iterator<char>(outFile)
);

(*) Extra pair of parentheses to prevent parsing this as function declaration.

For better performance you could read the file in chunks, using ifstream::read and write them with ofstream::write.

This is not the most optimal, but you can try something like:

std::ofstream outFile( "file.bin", ios_base::app | ios_base::out );

std::ifstream inFile(source_file_name, ios_base::binary | ios_base::in);
source >> noskipws;
char c;
while (inFile >> c) {
    outFile << c;
}

You can make probably make it more efficient by using a bigger buffer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top