473,395 Members | 1,452 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

convert wint_t to wchar_t?

I'm trying to use the getwc() function to get individual characters from a
stream but I've stumbled on a type conversion problem. The getwc() function
returns a type wint_t instead of a wchar_t and there isn't any information
on how to convert a wint_t type to wchar_t and vice-versa.

As the purpose of the wint_t type in that particular case is to serve as a
data type capable of storing all the possible values held by wchar_t along
with the WEOF value, I suspect that a simple cast could do the trick. Is
this true or could that approach break something?
Thanks in advance
Rui Maciel
Jul 3 '07 #1
1 6101
in comp.lang.c i read:
>I'm trying to use the getwc() function to get individual characters from a
stream but I've stumbled on a type conversion problem. The getwc() function
returns a type wint_t instead of a wchar_t and there isn't any information
on how to convert a wint_t type to wchar_t and vice-versa.
wchar_t and wint_t are the wide varieties of char and int, for character
handling purposes at least. assignment is sufficient -- you don't cast
char to int do you? -- and often you want wint_t rather than wchar_t
anyway, i.e., just as with getc you want to assign to an int not a char
likewise with getwc you want to assign to a wint_t not a wchar_t ...

basic "cat", narrow:

int c;

while (EOF != (c = getc(stdin)))
putc(c, stdout);

now wide:

wint_t c;

while (WEOF != (c = getwc(stdin)))
putwc(c, stdout);

remove repeated spaces, narrow:

int c, lastc = 0;

while (EOF != (c = getc(stdin))) {
if (' ' != c || c != lastc) putc(c, stdout);
lastc = c;
}

and wide:

wint_t c, lastc = 0;

while (WEOF != (c = getwc(stdin))) {
if (L' ' != c || c != lastc) putwc(c, stdout);
lastc = c;
}

surely there are many aspects that go beyond this[*], but at the level you
are asking about the same handling you do today with char and int works
equally well with wchar_t and wint_t. no casts needed.
[*] the first among many is that for wide characters to be effective you
must invoke setlocale() prior to using wide functions ...

#include <stdio.h>

int main(void) {
int c, lastc = 0;

while (EOF != (c = getc(stdin))) {
if (' ' != c || c != lastc) putc(c, stdout);
lastc = c;
}
return 0;
}

must needs expand to something more like:

#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <wchar.h>

int main(void) {
wint_t c, lastc = 0;

if (!setlocale(LC_CTYPE, "")) abort(); /* only character classification */

while (WEOF != (c = getwc(stdin))) {
if (L' ' != c || c != lastc) putwc(c, stdout);
lastc = c;
}
return 0;
}

--
a signature
Jul 3 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Bren | last post by:
Hi all, I wonder if anyone knows of a string class out there that can do on demand conversion from wchar_t to char? We are writing an app that will need to be localized and cross platform. ...
27
by: Trep | last post by:
Hi there! I've been having a lot of difficult trying to figure out a way to convert a terminated char array to a system::string for use in Visual C++ .NET 2003. This is necessary because I...
15
by: Yifan | last post by:
Hi Does anybody know how to convert System::String* to char*? I searched the System::String class members and did not find any. Thanks Yifan
6
by: gerg | last post by:
How would one convert a wstring to a string? This is what I have so far: bool copyto(std::string& l,std::wstring& r) { bool ret = false; size_t i = 0; const size_t n = r.length()+1;
8
by: davihigh | last post by:
My Friends: I am using std::ofstream (as well as ifstream), I hope that when i wrote in some std::string(...) with locale, ofstream can convert to UTF-8 encoding and save file to disk. So does...
4
by: uday.sen | last post by:
Hi, I need to convert a string from UTF8 to wide character (wchar_t *). I perform the same in windows using: MultiByteToWideChar(CP_UTF8, 0, pInput, -1, pOutput, nLen); However, in linux...
2
by: tchengwee | last post by:
Is there any function in visual c++ 6 to do the conversion from unicode to codepoint? In vb, ascw can be used to do that.Thanks
1
by: Rui Maciel | last post by:
I'm trying to use the getwc() function to get individual characters from a stream but I've stumbled on a type conversion problem. The getwc() function returns a type wint_t instead of a wchar_t and...
4
by: =?ISO-8859-2?Q?Boris_Du=B9ek?= | last post by:
Hi, I have an API that returns UTF-8 encoded strings. I have a utf8 codevt facet available to do the conversion from UTF-8 to wchar_t encoding defined by the platform. I have no trouble...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.