Getting to grips with wstring

First the basic question, (I) I'm using the following code, trying to print the 'á' character over the console without much luck (instead it prints a different character).

1
2
3
std::wstring temp;
temp = L"á";
std::wcout << temp << endl;


(II) I'm trying to learn how to do this, in order to read a file with characters like the following:

"á, é, í, ó, ú, Á, É, Í, Ó, Ú"

Using code similar to the following, the output is not what I expect it to be over the console.

1
2
3
4
5
6
7
8
9
10
11
12
13
std::vector<std::wstring> line;
std::wifstream file("file.txt");
std::wstring temp;

while(!file.eof())  // Store the file in a vector of strings,
{		    // where each string represents a line in the text file.
	getline(file, temp);
	line.push_back(temp);
}

int n = line.size();
for(int i = 0; i < n; i ++)
	std::wcout << i+1 << ": " << line[i] << std::endl;


Using this code, doesn't show the characters the way they should be shown.

I'm using the Visual C++ 2008 Express Edition Compiler, and seeing the output on a Windows XP console (is it possible that it has to do with the console).
It is because the console's code page doesn't match.

You need two functions (declared in <wincon.h>):
SetConsoleCP()
http://msdn.microsoft.com/en-us/library/ms686013(VS.85).aspx
SetConsoleOutputCP()
http://msdn.microsoft.com/en-us/library/ms686036(VS.85).aspx

The Unicode code page is 1200.

You can learn what other codepages your system supports from the registry.
http://msdn.microsoft.com/en-us/library/ms682064(VS.85).aspx

A list of code page identifiers
http://msdn.microsoft.com/en-us/library/ms776446.aspx

Good luck!
Thanks for your help Duoas,

At first I thought this was going to work, but for some reason I still can make it to properly display characters like the ones I showed in the original post.

I'm able to set some codepages, but not all of them, and at first I was not even being able to set the console to Unicode, I found that the Unicode code is 65001, as I'm not able to set it up with 1200.

I'll be posting more about it, if I find a solution.
Hmm, I'm sure I did this before...

But it must have been something GUI.

The console only uses "OEM" code pages, whereas everything else in windows uses "ANSI" codepages (no relation to OEM or ANSI). So apparently you must first convert from Unicode (or whatever you are using) to an "OEM" string.

Here's a little article that will help.
http://smallcode.weblogs.us/2006/10/25/code-page-for-win32-console-programs/

There is also a lot of good links from Wikipedia
http://en.wikipedia.org/wiki/Code_pages_on_the_Windows_OS

Sorry for the blithe advice earlier... :-S
Topic archived. No new replies allowed.