Note that there are some explanatory texts on larger screens.

plurals
  1. POreading binary files C++
    text
    copied!<p><p>I would like to ask for help ... I am starting in C++ and I got this homework at school ... We got to write function <code>bool UTF8toUTF16 (const char * src, const char * dst );</code> which is supposed to read <em>src</em> file coded in UTF-8 and write it into <em>dst</em> file but in UTF-16. We also mustn't use any other libraries than in my code down...<p> So the first thing I am trying to do is that I make a file "xx.txt" and in classic Windows notepad I write there for example char 'š'. Then am trying to write a program which reads each char of this file in binary mode byte by byte (or bytes by bytes) and prints it's value... but my program doesn't work like that...<p> So I have this file 'xx.txt' where is only 'š' which has UTF-8 value 'c5 a1', UTF-16 value '0161' and Unicode value '161' and I suppose result that it will print: i = 161 (hex) or something close to this result at least...<p> Here is my code so far:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;iomanip&gt; #include &lt;iostream&gt; #include &lt;fstream&gt; using namespace std; int main ( void ) { char name[] = "xx.txt"; fstream F ( name, ios::in | ios::binary ); unsigned int i; while( F.read ((char *) &amp; i, 2)) /* I dont know what size to write there - I would guess it s '2' - because I need 2 bytes for the char with hexUTF-16 code '0161', but 2 doesnt work*/ cout &lt;&lt; "i = " &lt;&lt; hex &lt;&lt; i &lt;&lt; " (hex) "; cout &lt;&lt; endl; F.close(); system("PAUSE"); return 0;} </code></pre> <p>Thanks in advance<p> Nikolas Jíša<p></p>
 

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