{"id":3844,"date":"2014-03-30T06:01:27","date_gmt":"2014-03-30T06:01:27","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/c-fstream-writing-a-binary-file-from-memory-file-contains-extra-byte-collection-of-common-programming-errors\/"},"modified":"2014-03-30T06:01:27","modified_gmt":"2014-03-30T06:01:27","slug":"c-fstream-writing-a-binary-file-from-memory-file-contains-extra-byte-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/c-fstream-writing-a-binary-file-from-memory-file-contains-extra-byte-collection-of-common-programming-errors\/","title":{"rendered":"C++ &lt;fstream&gt; Writing a binary file from memory, file contains extra byte?-Collection of common programming errors"},"content":{"rendered":"<ul>\n<li><img decoding=\"async\" src=\"http:\/\/i.msdn.microsoft.com\/dn186180.LOGO_Win1211(id-id,MSDN.10).png\" \/><br \/>\nmsdn Hi,I&#8217;m currently writing an assembler for a CPU we&#8217;ve been designing at university. It&#8217;s mostly working fine. I&#8217;ve just noticed a small error and I cannot find any reason why it would happen. My program writes the assebly binary into memory located by a pointer. I&#8217;ve debugged and it looks as it should:<\/p>\n<p>00 01 FF 00 02 20 00 03 04 05 03 00 06 03 00 07 03 00 08 09 0A 0B 0C 0D 0E 0F (Thats my entire 26 byte program in memory before I write to disk).<\/p>\n<p>Ok note the parts in red, in memory it is correct, but when I view my outputted file it looks like this:<\/p>\n<p>00 01 FF 00 02 20 00 03 04 05 03 00 06 03 00 07 03 00 08 09 0D 0A 0B 0C 0D 0E 0F (Thats the output file, a byte 0D has somehow appeared in the output).<\/p>\n<p>How could this be. I&#8217;m writing to file in the following way:<\/p>\n<p>\u00a0\u00a0\u00a0 fstream fp_out(&#8220;output.bin&#8221;, ios:<img decoding=\"async\" src=\"http:\/\/forums.microsoft.com\/MSDN\/emoticons\/emotion-3.gif\" \/>ut);<\/p>\n<p>\u00a0\u00a0\u00a0 for(int x = 0; x &lt; writebyte; x++)\u00a0\u00a0\u00a0 {\u00a0\u00a0\u00a0 \u00a0\u00a0 fp_out<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>msdn Hi,I&#8217;m currently writing an assembler for a CPU we&#8217;ve been designing at university. It&#8217;s mostly working fine. I&#8217;ve just noticed a small error and I cannot find any reason why it would happen. My program writes the assebly binary into memory located by a pointer. I&#8217;ve debugged and it looks as it should: 00 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3844","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3844","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=3844"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3844\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=3844"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=3844"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=3844"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}