Reputation: 6355
Code:
#include <fstream>
const wchar_t * testArray[] =
{
L"Wszystkie kategorie równoważne",
L"Oczekiwane przepływy pieniężne",
L"Risk i dojrzałość",
L"Pozycja strategiczna i lata na rynku",
L"Prawdopodobieństwo oszacowania"
};
void FaultyFunction(void)
{
std::wofstream file("test.txt");
for (int i = 0 ; i < 100 ; ++i)
{
for (int j = 0 ; j < 5 ; ++j)
{
file << testArray[j] << L'\t';
}
file << L'\n';
}
}
int main(void)
{
FaultyFunction();
return 0;
}
"test.txt" after execution:
Wszystkie kategorie równowa
(Yes, that's the whole file!)
Now, I don't know if that's a problem with std::wofstream
, file encoding or what but the result is at the very least strange. But when I remove polish letters:
const wchar_t * testArray[] =
{
L"Wszystkie kategorie rownowazne",
L"Oczekiwane przeplywy pieniezne",
L"Risk i dojrzalosc",
L"Pozycja strategiczna i lata na rynku",
L"Prawdopodobienstwo oszacowania"
};
The file content is as expected:
Wszystkie kategorie rownowazne Oczekiwane przeplywy pieniezne Risk i dojrzalosc Pozycja strategiczna i lata na rynku Prawdopodobienstwo oszacowania
Wszystkie kategorie rownowazne Oczekiwane przeplywy pieniezne Risk i dojrzalosc Pozycja strategiczna i lata na rynku Prawdopodobienstwo oszacowania
Wszystkie kategorie rownowazne Oczekiwane przeplywy pieniezne Risk i dojrzalosc Pozycja strategiczna i lata na rynku Prawdopodobienstwo oszacowania
... (it goes on like this for the remaining 97 lines)
And to add insult to injury when I use the very same testArray
(with polish chars) to write some things in an *.xls file (using libxl library) everything works just fine.
What's wrong? How can I save polish letters in a text file?
Edit:
Actually, when I tried the same code but with std::string
and std::ofstream
(not wide), it also worked fine (with polish letters). So what's the problem with the wide chars?
Upvotes: 2
Views: 1119
Reputation: 30113
Discussed on StackOverflow, MSDN, CodeGuru, CodeProject etc. dozens of times already. However, next code for a C++ console application prepared in Visual Studio 2013 could help:
#include "stdafx.h" // added by Visual Studio
#include <fstream>
#include <iostream>
#include <codecvt>
#include <locale>
#include <fcntl.h>
#include <io.h>
const std::locale utf8_locale = std::locale(std::locale(),
new std::codecvt_utf8<wchar_t>()); // prepare parameter for imbue call
const wchar_t* testArray[] =
{
L"Wszystkie kategorie równoważne",
L"Oczekiwane przepływy pieniężne",
L"Risk i dojrzałość",
L"Pozycja strategiczna i lata na rynku",
L"Prawdopodobieństwo oszacowania"
};
void FaultyFunction(void)
{
_setmode(_fileno(stdout), _O_U16TEXT); // for debugging; if omitted then
// std::wcout would be truncated as well
std::wofstream test_file("test.txt");
// test_file << L"\xEF\xBB\xBF"; // Byte Order Mark: not required nor recommended
// important: write BOM before imbue
test_file.imbue(utf8_locale); // set the locale of the stream (and buffer
// if any) to the specified locale
for (int i = 0; i < 10; ++i)
{
for (int j = 0; j < 5; ++j)
{
test_file << testArray[j] << L'\t';
// std::wcout << testArray[j] << L'\n'; // for debugging
}
test_file << L'\n';
}
}
int main(void)
{
FaultyFunction();
return 0;
}
Upvotes: 1