Levno_710
Levno_710

Reputation: 86

c++ concat files

I am trying to concatenate two binary files into another output file. I am using the following code:

std::ofstream outFile("file.out", std::ios::ate );
std::ifstream inFile1(getExeDirectory() / "file.in");
std::ifstream inFile2(archive_file);

outFile << inFile1.rdbuf() << inFile2.rdbuf();

std::cout << "Created: '" << out_file << "'" << std::endl;

inFile1.close();
inFile2.close();
outFile.close();

// Delete tmp file
fs::remove(archive_file);

std::cout << "Done!" << std::endl;

The problem I am facing, is that archive_file is a temp file, that I need to delete after it has been appended. My program only copies the first 2-3kb of file.in before printing "Done!" and terminating.

file.in is 1.2Mb, while archive_file is 4.7kB. The output contains the first 1400 bytes of file.in, followed by the first 52 bytes of archive_file.

How would I fix this?

Upvotes: 1

Views: 289

Answers (1)

Levno_710
Levno_710

Reputation: 86

The issue was, that my files contained null bytes, and I didn't open them in binary mode. This fixed the issue:

std::ofstream outFile("file.out", std::ios_base::binary );
std::ifstream inFile1(getExeDirectory() / "file.in", std::ios_base::binary);
std::ifstream inFile2(archive_file, std::ios_base::binary);

Upvotes: 3

Related Questions