I'm trying to write strings with non-ASCII characters in it to a file, such as "maçã", "pé", and so on.
I'm currently doing something like this:
_setmode(_fileno(stdout), _O_U16TEXT);
//I added the line above recently to the question,
//but it was in the code before, I forgot to write it
//I also included some header files, to be able to do that
//can't really remember which, if necessary I'll look it up.
wstring word=L"";
wstring file = L"example_file.txt"
vector<wstring> my_vector;
wofstream my_output(file);
while(word != L".")
{
getline(wcin, word);
if(word!= L".")
my_vector.pushback(word);
}
for(std::vector<wstring>::iterator j=my_vector.begin(); j!=my_vector.end(); j++)
{
my_output << *j << endl;
//element pointed by iterator going through the whole vector
my_output << L("maçã pé") << endl;
}
my_output.close();
Now, if I enter "maçã", "pé" and "." as words (only the 1st two are stored in the vector), the output to the file is rather strange:
- the words I entered (stored in variables) appear strange: "ma‡Æ" and "p,";
- the words stored directly in the code appear perfectly normal "maçã pé";
I have tried using wcin >> word
instead of getline(wcin, word)
and writing to the console instead of a file, the results are the same: writes variable strings wrong, writes strings directly in code perfectly.
I cannot find a reason for this to happen, so any help will be greatly appreciated.
Edit: I am working in Windows 7, using Visual C++ 2010
Edit 2: added one more line of code, that I had missed. (right in the beginning)
EDIT 3: following SigTerm's suggestion, I realised the problem is with the input: neither wcin nor getline are getting the strings with right formatting to variable wstring word
. So, the question is, do you know what is causing this or how to fix it?