Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>What if instead two wstring's (as I have now) I want to have one wstring and one string member variable in my class? (I think in such case my code won't work?).</p> </blockquote> <p>There are an inserter defined for <code>char *</code> for any <code>basic_ostream</code> (<code>ostream</code> and <code>wostream</code>), so you can use the result of <code>c_str()</code> member function call for the <code>string</code> member. For example, if the <code>string</code> member is <code>other_data_</code>:</p> <pre><code> return os &lt;&lt; p.price_ &lt;&lt; endl &lt;&lt; p.product_index_ &lt;&lt; endl &lt;&lt; p.product_name_ &lt;&lt; endl &lt;&lt; p.other_data_.c_str() &lt;&lt; endl; </code></pre> <p>The extractor case is more complex, since you'll have to read as <code>wstring</code> and the convert to <code>string</code>. The most simple way to do this is just reading as <code>wstring</code> and then narrowing each character:</p> <pre><code>wstring temp; getline(is, temp); p.other_data_ = string(temp.begin(), temp.end()); </code></pre> <p>I'm not using locales in this sample, just converting a sequence of bytes (8 bits) to a sequence of words (16 bits) for output and the opposite (truncating values) for input. That is OK if you are using ASCII chars, or using single-byte chars and you don't require an specific format (as Unicode) for output.</p> <p>Otherwise, you will need handle with <code>locale</code>s. locale gives cultural contextual information to interpret the string (remember that is just a sequence of bytes, not characters in the sense of letters or symbols; the map between the bytes and what symbol represents is defined by the <code>locale</code>). <code>locale</code> is not an very easy to use concept (human culture isn't too). As you suggest yourself, it would be better make first some investigation about how it works.</p> <p>Anyway, the idea is:</p> <ol> <li>Identify the charset used in string and the charset used in file (Unicode or utf-16).</li> <li>Convert the <code>string</code>s from original charset to Unicode using <code>locale</code> for output.</li> <li>Convert the <code>wstring</code>s read from file (in Unicode) to <code>string</code>s using <code>locale</code>.</li> </ol> <blockquote> <p>Finally, below, in main, when I initialize s2.product_name_= L"megatex"; if instead of megatex I write something in Russian say (e.g., <code>s2.product_name_= L"логин"</code>), the code doesn't work anymore as intended.</p> </blockquote> <p>When you define an array of <code>wchar_t</code> using <code>L""</code>, you'are not really specifying the string is Unicode, just that the array is of chars, not wchar_t. I suppose the intended working is <code>s2.product_name_</code> store the name in Unicode format, but the compiler will take every <code>char</code> in that string (as without <code>L</code>) and convert to <code>wchar_t</code> just padding with zeros the most significant byte. Unicode is not good supported in the C++ standard until C++11 (and is still not really too supported). It works just for ASCII characters because they have the same codification in Unicode (or UTF-8).</p> <p>For using the Unicode characters in a static string, you can use escape characters: <code>\uXXXX</code>. Doing that for every not-English character is not very comfortable, I know. You can found a list of Unicode characters in multiple sites in the web. For example, in the Wikipedia: <a href="http://en.wikipedia.org/wiki/List_of_Unicode_characters" rel="nofollow">http://en.wikipedia.org/wiki/List_of_Unicode_characters</a>.</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.
    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