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)

 

USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.

Comments

Your email address will not be published. Required fields are marked *