Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your code working as expected: <code>char* data</code> array contains file's data. What's going wrong here is that your <code>char* data</code> array is misinterpreted by your visualizers (whatever you use to visualize: debugger visualizer, <code>std::cout</code>, etc). They all try to print null-terminated (c-style) string, but it terminates instantly, as first char is 0. Raw arrays can also be visualized in debuggers as pointers: address and only first data member's value (because it cannot know where array ends). In C# situation is different as arrays are objects there, much like <code>std::vector</code>s, so their size is known.</p> <p><em>Offtopic (sorry for that):</em></p> <p>I would like to comment your second, native C++ <code>BasicReader::ReadData</code> method implementation, as it hurts my C++ feelings ;) You trying to write C code in C++11 style. "There is more than one way to skin a cat", but there are some advices:</p> <ul> <li>don't use raw pointers (char*), use STL containers instead (<code>std::vector</code>, <code>std::string</code>)</li> <li>have you really good reason to use <code>std::unique_ptr&lt;uint8_t[]&gt; data</code> + <code>size_t dataSize</code> instad of <code>std::vector&lt;uint8_t&gt;</code>?</li> <li>avoid using raw <code>operator new()</code>, use STL containers, <code>std::make_shared</code>, <code>std::make_unique</code> (if available)</li> <li><code>seekg()</code>+<code>tellg()</code> file size counting can report wrong size in case of big files</li> </ul> <p>Doesn't this code looks a little cleaner and more safe:</p> <pre><code>std::vector&lt;uint8_t&gt; ReadData(const std::string filename) { std::vector&lt;uint8_t&gt; data; std::ifstream fs; fs.open(filename, std::ifstream::in | std::ifstream::binary); if (fs.good()) { auto size = FileSize(filename); // TODO: check here if size is more than size_t data.resize(static_cast&lt;size_t&gt;(size)); fs.seekg(0, std::ios::beg); fs.read(reinterpret_cast&lt;char*&gt;(&amp;data[0]), size); fs.close(); } return data; } </code></pre> <p>And usage is even more cleaner:</p> <pre><code>std::vector&lt;uint8_t&gt; vertexShaderData = ReadData("VertexShader.cso"); if(vertexShaderData.empty()) { /* handle it*/ } auto wannaKnowSize = vertexShaderData.size(); </code></pre> <p>As a bonus, you got a nice-looking debugger visualization.</p> <p>And safe <code>FileSize()</code> implementation. You can use either boost::filesystem, of <code>std::tr2</code> if your STL had implemented it.</p> <pre><code>#include &lt;filesystem&gt; namespace filesystem = std::tr2::sys; /* or namespace filesystem = boost::filesystem */ uintmax_t FileSize(std::string filename) { filesystem::path p(filename); if (filesystem::exists(p) &amp;&amp; filesystem::is_regular_file(p)) return filesystem::file_size(p); return 0; } </code></pre> <p>Hope it helps somehow.</p>
    singulars
    1. This table or related slice is empty.
    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.
    1. VO
      singulars
      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