Combine Files

fstream is fast and is good for combining large files together without needing to read in each file.

Combine 2 files (can be any file type):

#include <fstream>
	std::ifstream ifstream1("a.bin", std::ios_base::binary);
	std::ifstream ifstream2("b.bin", std::ios_base::binary);
	std::ofstream ofstream1("c.bin", std::ios_base::binary);

	ofstream1 << ifstream1.rdbuf() << ifstream2.rdbuf();

Adding some bytes to the output file:


	BYTE MyByteArray[3];
	MyByteArray[0] = 0x01;
	MyByteArray[0] = 0x02;
	MyByteArray[0] = 0x03;
	ofstream1.write((const char*)&MyByteArray[0], 3);
	ofstream1.close();

Create a new text file from an existing text file and a string


#include <fstream>
	std::ifstream ifstream1("some_existing_file.txt", std::ios_base::in);
	std::ofstream output_file("my_new_file.txt");				//Will overwrite an existing file

	output_file << ifstream1.rdbuf();
	output_file << "My text to add";
	output_file.close();			//(Not strictly necessary as it will get closed when output_file goes out of scope)