{"id":1059,"date":"2016-01-06T16:51:29","date_gmt":"2016-01-06T16:51:29","guid":{"rendered":"http:\/\/coolt.ch\/notizen\/?p=1059"},"modified":"2016-01-07T07:38:52","modified_gmt":"2016-01-07T07:38:52","slug":"binary-output-c","status":"publish","type":"post","link":"https:\/\/coolt.ch\/notizen\/binary-output-c\/","title":{"rendered":"binary output C++"},"content":{"rendered":"<p><strong>Problem mit &lt;&lt;<\/strong> (Standard filestream Operator)<\/p>\n<p>Mit Bitset und &lt;&lt; in einen Outputfilestream wurde jedes 0 und jedes 1 zu einem Ascii gewandelt.<\/p>\n<pre class=\"lang:sh decode:true\">std::bitset&lt;16&gt; left (161);\r\nstd::bitset&lt;8&gt; symbol(98);\r\n\r\nstd::ofstream ofs;\r\nofs.open(\"treebinary.bin\", std::ofstream::binary) \r\n\r\nofs &lt;&lt; left &lt;&lt; symbol ;\r\n\r\nofs.close()<\/pre>\n<p>Dateigr\u00f6sse (16 + 8) * 1 Char = 24 Byte. (Faktor 8 zu gross).<\/p>\n<p><strong>L\u00f6sung write( Pointer to Data, size)<\/strong><br \/>\nIn der Funktion write kann ein Pointer \u00fcbergeben werden. Dadurch findet keine Umwandlung in String (durch &lt;&lt;) statt. Die 0 und 1er werden direkt bin\u00e4r geschrieben.<\/p>\n<pre class=\"lang:sh decode:true\">char data[3]    \/\/ 3 Bytes\r\n\r\n\/\/ inhalt von 1 Byte und 2 Bytes\r\nstd::bitset&lt;16&gt; left (361);\r\nstd::bitset&lt;8&gt; symbol(98);\r\n\r\n\/\/ array f\u00fcllen\r\ndata[0] = left;\r\ndata[2] = symbol;\r\n\r\n\/\/array in 1mal ausgeben\r\nstd::ofstream ofs; ofs.write((char *) data , 3) \r\n\r\nofs.close()<\/pre>\n<ul>\n<li>Alle Bits werden auf einmal geschrieben<\/li>\n<li>kein &lt;&lt; left &lt;&lt; symbol notwendig<\/li>\n<li>Man gibt den Pointer mit und die L\u00e4nge der Daten<\/li>\n<\/ul>\n<pre class=\"lang:sh decode:true\">char* data[3]    \/\/ initalisiert als Pointer\r\n\r\nstd::bitset&lt;16&gt; left (361);\r\nstd::bitset&lt;8&gt; symbol(98);\r\n\r\ndata[0] = left;\r\ndata[2] = symbol;\r\n\r\nstd::ofstream ofs; ofs.write(data , sizeof() \r\n\r\nofs.close()<\/pre>\n<pre class=\"lang:sh decode:true \">\/\/ array of struct\r\nstd::struct Node\r\n{  \r\n    uint16_t left = 0;\r\n    uint16_t right = 0;\r\n    uint8_t symbol = 0;\r\n    bool filled = false;\r\n};\r\n\r\nNode tree[100];\r\n\r\n\/\/ f\u00fcllen des arrays per tree-funktion\r\n\r\n\r\n\/\/ array per Pointer mitgeben, l\u00e4nge = Node-l\u00e4nge * Anzahl\r\nstd::ofstream ofs; ofs.write((char*) tree , sizeof( (struct Node) * 100);\r\n\r\nofs.close()<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Problem mit &lt;&lt; (Standard filestream Operator) Mit Bitset und &lt;&lt; in einen Outputfilestream wurde jedes 0 und jedes 1 zu einem Ascii gewandelt. std::bitset&lt;16&gt; left (161); std::bitset&lt;8&gt; symbol(98); std::ofstream ofs; ofs.open(&#8222;treebinary.bin&#8220;, std::ofstream::binary) ofs &lt;&lt; left &lt;&lt; symbol ; ofs.close() Dateigr\u00f6sse (16 + 8) * 1 Char = 24 Byte. (Faktor 8 zu gross). L\u00f6sung write( &hellip; <a href=\"https:\/\/coolt.ch\/notizen\/binary-output-c\/\" class=\"more-link\"><span class=\"screen-reader-text\">binary output C++<\/span> weiterlesen<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[29],"tags":[18,6],"_links":{"self":[{"href":"https:\/\/coolt.ch\/notizen\/wp-json\/wp\/v2\/posts\/1059"}],"collection":[{"href":"https:\/\/coolt.ch\/notizen\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coolt.ch\/notizen\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coolt.ch\/notizen\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coolt.ch\/notizen\/wp-json\/wp\/v2\/comments?post=1059"}],"version-history":[{"count":5,"href":"https:\/\/coolt.ch\/notizen\/wp-json\/wp\/v2\/posts\/1059\/revisions"}],"predecessor-version":[{"id":1064,"href":"https:\/\/coolt.ch\/notizen\/wp-json\/wp\/v2\/posts\/1059\/revisions\/1064"}],"wp:attachment":[{"href":"https:\/\/coolt.ch\/notizen\/wp-json\/wp\/v2\/media?parent=1059"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coolt.ch\/notizen\/wp-json\/wp\/v2\/categories?post=1059"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coolt.ch\/notizen\/wp-json\/wp\/v2\/tags?post=1059"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}