Assuming you don’t want to do any processing, and just want to combine the two files to do the third, you can do it very simply, file buffer streams:
std::ifstream if_a("a.txt", std::ios_base::binary); std::ifstream if_b("b.txt", std::ios_base::binary); std::ofstream of_c("c.txt", std::ios_base::binary); of_c << if_a.rdbuf() << if_b.rdbuf();
I have tried such things with files up to 100 MB in the past and had no problems. You effectively allow C ++ and libraries to handle the required buffering. It also means you don’t have to worry about file position if your files get really big.
An alternative is that you just wanted to copy b.txt to the end of a.txt , in which case you would need to open a.txt using the append flag and search to the end:
std::ofstream of_a("a.txt", std::ios_base::binary | std::ios_base::app); std::ifstream if_b("b.txt", std::ios_base::binary); of_a.seekp(0, std::ios_base::end); of_a << if_b.rdbuf();
How these methods work by passing std::streambuf input streams to operator<< output stream, one of the overrides of which takes the streambuf parameter ( operator << ). As mentioned in this link, in the absence of errors, streambuf inserted unformatted into the output stream to the end of the file.
source share