Read wchar_t from stream

Jul 13, 2008 at 7:37pm
Hello,
I'm trying to read a wchar_t from cin but I always have the following compiler error "binary '>>' : no operator found which takes a right-hand operand of type 'wchar_t' (or there is no acceptable conversion)"

I used the following:
1
2
3
4
5
6
7
#include <iostream>
using namespace std;

int main(){
   wchar_t ch;
   cin >> ch;
}



When I use the following:
1
2
3
4
5
6
7
8
#include <iostream>
#include <conio.h>
using namespace std;
int main(){
   wchar_t ch;
   ch = _getwch();
   cout << ch;
}

instead of my character it shows a number that I don't even know what it represents...(it is not the ascii code of that letter)

Can anybody please tell me how to read wchar_t from cin and also how to use them in wstring?
Jul 13, 2008 at 8:12pm
Unfortunately, the standard I/O streams must match the input character type, traits, and allocator to be used.

Fortunately, there exist wcin and wcout etc, which work with wide characters.

I've never used them, actually, but I think it should get you what you want.
1
2
3
4
5
6
7
#include <iostream>
using namespace std;

int main(){
   wchar_t ch;
   wcin >> ch;
}


Hope this helps.
Jul 13, 2008 at 8:32pm
Yes, it helped,
I found out that reading from a file is a little bit more complicated than that because I have to do my own encoding and decoding of the file...
Anyway, thanx for your answer.
Jul 13, 2008 at 9:14pm
Don't forget wifstream and wofstream. :-)
Jul 13, 2008 at 10:33pm
I was looking for it and couldn't find it :P...
I tried "iwfstream" and "ifwstream" but I didn't think that every wide char action has the "w" at the beggining.
Thanx again...
Last edited on Jul 13, 2008 at 10:34pm
Jul 14, 2008 at 6:57am
closed account (z05DSL3A)
Standard objects
As part of the iostream library, the header file <iostream> declares certain objects that are used to perform input and output operations on the standard input and output.
They are divided in two sets: narrow-oriented objects, which are the popular cin, cout, cerr and clog and their wide-oriented counterparts, declared as wcin, wcout, wcerr and wclog.
From: http://www.cplusplus.com/reference/iostream/

I'll let you Google for wifstream and wofstream...

Last edited on Jul 14, 2008 at 7:06am
Jul 14, 2008 at 9:17am
From MSDN:
wfstream
A type basic_fstream specialized on wchar_t template parameters.
wifstream
A type basic_ifstream specialized on wchar_t template parameters.
wofstream
A type basic_ofstream specialized on wchar_t template parameters.
wfilebuf
A type basic_filebuf specialized on wchar_t template parameters.


You see, if somebody tells you that it exists it's easier to find it. I'll look at msdn next time because it has all of them under the <fstream> column so you can easier find it.
I had no clue that wcin, wcout, wcerr and wclog exist until Duoas told me and then I started thinking about "w" almost on every other function.
Topic archived. No new replies allowed.