{"id":2689,"date":"2016-11-05T21:14:27","date_gmt":"2016-11-05T21:14:27","guid":{"rendered":"https:\/\/raspberry-projects.com\/pi\/?p=2689"},"modified":"2018-04-24T14:25:43","modified_gmt":"2018-04-24T14:25:43","slug":"combining-files","status":"publish","type":"post","link":"https:\/\/raspberry-projects.com\/pi\/programming-in-c\/file-input-and-output\/file-operations\/combining-files","title":{"rendered":"Combining Files"},"content":{"rendered":"<h4>\nCombine&nbsp;Files<br \/>\n<\/h4>\n<p>\nfstream&nbsp;is fast and is good for combining large files together&nbsp;without needing to read in each file.\n<\/p>\n<h5>\nCombine 2 files (can be any file type):<br \/>\n<\/h5>\n<pre>\r\n<code>\r\n#include &lt;fstream&gt;\r\n\tstd::ifstream ifstream1(&quot;a.bin&quot;, std::ios_base::binary);\r\n\tstd::ifstream ifstream2(&quot;b.bin&quot;, std::ios_base::binary);\r\n\tstd::ofstream ofstream1(&quot;c.bin&quot;, std::ios_base::binary);\r\n\r\n\tofstream1 &lt;&lt; ifstream1.rdbuf() &lt;&lt; ifstream2.rdbuf();\r\n<\/code><\/pre>\n<p>\nAdding some bytes to the output file:\n<\/p>\n<pre>\r\n<code>\r\n\tBYTE MyByteArray[3];\r\n\tMyByteArray[0] = 0x01;\r\n\tMyByteArray[0] = 0x02;\r\n\tMyByteArray[0] = 0x03;\r\n\tofstream1.write((const char*)&amp;MyByteArray[0], 3);\r\n\tofstream1.close();\r\n<\/code><\/pre>\n<h4>\nCreate a new text file from an&nbsp;existing text file and a string<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n#include &lt;fstream&gt;\r\n\tstd::ifstream ifstream1(&quot;some_existing_file.txt&quot;, std::ios_base::in);\r\n\tstd::ofstream output_file(&quot;my_new_file.txt&quot;);\t\t\t\t\/\/Will overwrite an existing file\r\n\r\n\toutput_file &lt;&lt; ifstream1.rdbuf();\r\n\toutput_file &lt;&lt; &quot;My text to add&quot;;\r\n\toutput_file.close();\t\t\t\/\/(Not strictly necessary as it will get closed when output_file goes out of scope)\r\n<\/code><\/pre>\n<p>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Combine&nbsp;Files fstream&nbsp;is fast and is good for combining large files together&nbsp;without needing to read in each file. Combine 2 files (can be any file type): #include &lt;fstream&gt; std::ifstream ifstream1(&quot;a.bin&quot;, std::ios_base::binary); std::ifstream ifstream2(&quot;b.bin&quot;, std::ios_base::binary); std::ofstream ofstream1(&quot;c.bin&quot;, std::ios_base::binary); ofstream1 &lt;&lt; ifstream1.rdbuf() &lt;&lt; ifstream2.rdbuf(); Adding some bytes to the output file: BYTE MyByteArray[3]; MyByteArray[0] = 0x01; MyByteArray[0] = [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[135,141],"tags":[],"class_list":["post-2689","post","type-post","status-publish","format-standard","hentry","category-file-operations","category-stringstream"],"_links":{"self":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/2689","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/comments?post=2689"}],"version-history":[{"count":6,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/2689\/revisions"}],"predecessor-version":[{"id":2869,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/2689\/revisions\/2869"}],"wp:attachment":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/media?parent=2689"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/categories?post=2689"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/tags?post=2689"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}