Note that there are some explanatory texts on larger screens.

plurals
  1. POC++, Writing vector<char> to ofstream skips whitespace
    primarykey
    data
    text
    <p>Despite my sincerest efforts, I cannot seem to locate the bug here. I am writing a vector to an ofstream. The vector contains binary data. However, for some reason, when a whitespace character (0x10, 0x11, 0x12, 0x13, 0x20) is supposed to be written, it is skipped.</p> <p>I have tried using iterators, and a direct ofstream::write().</p> <p>Here is the code I'm using. I've commented out some of the other methods I've tried.</p> <pre><code>void write_file(const std::string&amp; file, std::vector&lt;uint8_t&gt;&amp; v) { std::ofstream out(file, std::ios::binary | std::ios::ate); if (!out.is_open()) throw file_error(file, "unable to open"); out.unsetf(std::ios::skipws); /* ostreambuf_iterator ... std::ostreambuf_iterator&lt;char&gt; out_i(out); std::copy(v.begin(), v.end(), out_i); */ /* ostream_iterator ... std::copy(v.begin(), v.end(), std::ostream_iterator&lt;char&gt;(out, "")); */ out.write((const char*) &amp;v[0], v.size()); } </code></pre> <p>EDIT: And the code to read it back.</p> <pre><code>void read_file(const std::string&amp; file, std::vector&lt;uint8_t&gt;&amp; v) { std::ifstream in(file); v.clear(); if (!in.is_open()) throw file_error(file, "unable to open"); in.unsetf(std::ios::skipws); std::copy(std::istream_iterator&lt;char&gt;(in), std::istream_iterator&lt;char&gt;(), std::back_inserter(v)); } </code></pre> <p>Here is an example input:</p> <pre><code>30 0 0 0 a 30 0 0 0 7a 70 30 0 0 0 32 73 30 0 0 0 2 71 30 0 0 4 d2 </code></pre> <p>And this is the output I am getting when I read it back:</p> <pre><code>30 0 0 0 30 0 0 0 7a 70 30 0 0 0 32 73 30 0 0 0 2 71 30 0 0 4 d2 </code></pre> <p>As you can see, 0x0a is being ommited, ostensibly because it's whitespace.</p> <p>Any suggestions would be greatly appreciated.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload