If I read special characters from a file and then try to compare them (like with an if) it doesn't recognize them.
std::wstring c;
std::wifstream file;
file.open("test.txt");
while (file)
{
wchar_t tmp = file.get();
c += tmp;
}
file.close();
size_t l = c.length();
for (int i = 0; i < l; i++)
{
wchar_t a = c[i];
if (a == L'ä') {
std::cout << "if triggered.";
}
}
But when I create a wchar and predefine a special character it does work.
wchar_t a = L'ä';
if (a == L'ä') {
std::cout << "if triggered";
}
and if I put the wstring that was loaded from the file, in the file I get the text back. Nothing weird happens there.
This depends on the kind of file encoding. I would implicitly say that, in this case, UTF-8.
The code below may be work fine:
However,
std::codecvt_utf8is deprecated in C++17For the cases using higher C++17:
By MSVC++
I recommend using
CString, it's too easy and worked on every almost version of C++, follow this:#include <atlstr.h>for non-MFC