473,657 Members | 2,566 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

wstring & wifstream

Hi,
I have my program using wstring everywhere instead of string.
Similarly I need to process some file, which contains unicode or ascii
character. I need to stream them. Thus I use wifstream etc. However the
open member function of is not templated, and use const char* as
filename. I have my filename as wstring, where c_str() returns const
wchar_t* type. Thus how to convert a wstring to string or const char*
and pass to open of wifstream?

Sep 29 '06 #1
9 9610

"toton" <ab*******@gmai l.comschrieb im Newsbeitrag
news:11******** *************@m 7g2000cwm.googl egroups.com...
Hi,
I have my program using wstring everywhere instead of string.
Similarly I need to process some file, which contains unicode or
ascii
character. I need to stream them. Thus I use wifstream etc. However
the
open member function of is not templated, and use const char* as
filename. I have my filename as wstring, where c_str() returns const
wchar_t* type. Thus how to convert a wstring to string or const
char*
and pass to open of wifstream?
I know of functions:
mbtowc and wctomb. I don't know if they are std, nor do I know of a
C++ pendant.
Sep 29 '06 #2
toton wrote:
Hi,
I have my program using wstring everywhere instead of string.
Similarly I need to process some file, which contains unicode or ascii
character. I need to stream them. Thus I use wifstream etc. However the
open member function of is not templated, and use const char* as
filename. I have my filename as wstring, where c_str() returns const
wchar_t* type. Thus how to convert a wstring to string or const char*
and pass to open of wifstream?
You need to know a bit about the locale settings, but frankly they
could be almost anything. If you're lucky you may find some compiler
specific details on what form the const char * should be. It is a shame
that this part of the standard is still living in the older C universe.

You don't say which platform you're using but the wide character
streams are badly broken on Microsoft's compiler. In practice you
cannot write Unicode characters to them.

There is a part of the Boost library that deals with these issus, but
I've only taken a quick look at it and haven't actually tried them yet.
Kirit

Sep 29 '06 #3

Kirit Sælensminde wrote:
toton wrote:
Hi,
I have my program using wstring everywhere instead of string.
Similarly I need to process some file, which contains unicode or ascii
character. I need to stream them. Thus I use wifstream etc. However the
open member function of is not templated, and use const char* as
filename. I have my filename as wstring, where c_str() returns const
wchar_t* type. Thus how to convert a wstring to string or const char*
and pass to open of wifstream?

You need to know a bit about the locale settings, but frankly they
could be almost anything. If you're lucky you may find some compiler
specific details on what form the const char * should be. It is a shame
that this part of the standard is still living in the older C universe.

You don't say which platform you're using but the wide character
streams are badly broken on Microsoft's compiler. In practice you
cannot write Unicode characters to them.
Most of the cases I don't need to read wide charater from stream.
However I am passing the file name for reading which can be wchat type
(i.e only the file name, not the file itself). So file.open() is
creating problem. I pass the file name from some GUI (wxWidgets) which
has a string type of unicode build, a thin wrapper over STL string.n
Thus it's c_str() returns a const wchar_t*. Unfortunately I am using
Windows & visual studio 2003. I had expected a template open function
for ifstream or it's base! .even if ifstream can open the file with the
filename as wide char is fine form me (My file is still ascii, though
file format specifies nothing about encoding). The GUI library
(wxWidgets) gives a function to convert wide char to char type using
current codepage , but I am not sure still will it be able to take care
of languages other than english.
There is a part of the Boost library that deals with these issus, but
I've only taken a quick look at it and haven't actually tried them yet.
can you give a reference to the section is boost?

thanks

Kirit
Sep 29 '06 #4
"Kirit Sælensminde" <ki************ ****@gmail.comw rote in message
news:11******** **************@ m7g2000cwm.goog legroups.com...
toton wrote:
>Hi,
I have my program using wstring everywhere instead of string.
Similarly I need to process some file, which contains unicode or ascii
character. I need to stream them. Thus I use wifstream etc. However the
open member function of is not templated, and use const char* as
filename. I have my filename as wstring, where c_str() returns const
wchar_t* type. Thus how to convert a wstring to string or const char*
and pass to open of wifstream?

You need to know a bit about the locale settings, but frankly they
could be almost anything. If you're lucky you may find some compiler
specific details on what form the const char * should be. It is a shame
that this part of the standard is still living in the older C universe.

You don't say which platform you're using but the wide character
streams are badly broken on Microsoft's compiler. In practice you
cannot write Unicode characters to them.
In practice they're not broken and you can write Unicode characters.
As with any other Standard C++ library, you need an appropriate
codecvt facet for the code conversion you favor. See our add-on
library, which includes a broad assortment.
There is a part of the Boost library that deals with these issus, but
I've only taken a quick look at it and haven't actually tried them yet.
P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Sep 30 '06 #5
P.J. Plauger wrote:
"Kirit Sælensminde" <ki************ ****@gmail.comw rote in message
news:11******** **************@ m7g2000cwm.goog legroups.com...
toton wrote:
Hi,
I have my program using wstring everywhere instead of string.
Similarly I need to process some file, which contains unicode or ascii
character. I need to stream them. Thus I use wifstream etc. However the
open member function of is not templated, and use const char* as
filename. I have my filename as wstring, where c_str() returns const
wchar_t* type. Thus how to convert a wstring to string or const char*
and pass to open of wifstream?
You need to know a bit about the locale settings, but frankly they
could be almost anything. If you're lucky you may find some compiler
specific details on what form the const char * should be. It is a shame
that this part of the standard is still living in the older C universe.

You don't say which platform you're using but the wide character
streams are badly broken on Microsoft's compiler. In practice you
cannot write Unicode characters to them.

In practice they're not broken and you can write Unicode characters.
As with any other Standard C++ library, you need an appropriate
codecvt facet for the code conversion you favor. See our add-on
library, which includes a broad assortment.
I presume that I'm just going to show my ignorance of the iostreams
library, but I don't mind that if I can learn how to do this correctly.

Because the OP's question is about wide character filenames rather than
writing wide characters to streams I'm going to start a different
thread as I don't want to hijack this one (any further).
K

Sep 30 '06 #6
"Kirit Sælensminde" <ki************ ****@gmail.comw rote in message
news:11******** **************@ e3g2000cwe.goog legroups.com...

P.J. Plauger wrote:
"Kirit Sælensminde" <ki************ ****@gmail.comw rote in message
news:11******** **************@ m7g2000cwm.goog legroups.com...
toton wrote:
Hi,
I have my program using wstring everywhere instead of string.
Similarly I need to process some file, which contains unicode or ascii
character. I need to stream them. Thus I use wifstream etc. However the
open member function of is not templated, and use const char* as
filename. I have my filename as wstring, where c_str() returns const
wchar_t* type. Thus how to convert a wstring to string or const char*
and pass to open of wifstream?
You need to know a bit about the locale settings, but frankly they
could be almost anything. If you're lucky you may find some compiler
specific details on what form the const char * should be. It is a shame
that this part of the standard is still living in the older C universe.

You don't say which platform you're using but the wide character
streams are badly broken on Microsoft's compiler. In practice you
cannot write Unicode characters to them.

In practice they're not broken and you can write Unicode characters.
As with any other Standard C++ library, you need an appropriate
codecvt facet for the code conversion you favor. See our add-on
library, which includes a broad assortment.
I presume that I'm just going to show my ignorance of the iostreams
library, but I don't mind that if I can learn how to do this correctly.

Because the OP's question is about wide character filenames rather than
writing wide characters to streams I'm going to start a different
thread as I don't want to hijack this one (any further).

Right, it was, and I forgot to address that point too. The latest
VC++ library, like our add-on library, lets you open files by
name specified as a wchar_t * string.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Sep 30 '06 #7
"P.J. Plauger" <pj*@dinkumware .comwrites:
In practice they're not broken and you can write Unicode characters.
As with any other Standard C++ library, you need an appropriate
codecvt facet for the code conversion you favor. See our add-on
library, which includes a broad assortment.
This is probably a bit OT, but I'm wondering why you would have to set
this up manually. I don't know what the language requirements are
(there are probably none, as usual), but on unix platforms

std::cout.imbue ( std::locale("") );

usually inherit the locale from the environment, _except_ for the
character encoding as it appears.

However,

setlocale(LC_AL L, "");

does inherit even the code conversion from the environment on my
system.

Test case:

#include <iostream>
#include <cstdio>

using namespace std;

int main(int argc, char** argv)
{
cout.imbue( locale("") );

// 0xc3 0xa4 is UTF8 for 0xe4, which is ä

cout << " cout: " << char(0xc3) << char(0xa4) << endl; //prints ä
wcout << L"wcout: " << wchar_t(0xe4) << endl; //doesn't

setlocale(LC_AL L, "");

char const* narrow = "\xc3\xa4";
wchar_t const* wide = L"\xe4";

printf("printf narrow: %s\n", narrow); //prints ä
printf("printf wide: %ls\n", wide); //does likewise
}

I presume this is just my broken platform?

Regards,

Jens
Sep 30 '06 #8
"Jens Theisen" <jt***@arcor.de wrote in message
news:87******** ****@arcor.de.. .
"P.J. Plauger" <pj*@dinkumware .comwrites:
>In practice they're not broken and you can write Unicode characters.
As with any other Standard C++ library, you need an appropriate
codecvt facet for the code conversion you favor. See our add-on
library, which includes a broad assortment.

This is probably a bit OT, but I'm wondering why you would have to set
this up manually. I don't know what the language requirements are
(there are probably none, as usual), but on unix platforms

std::cout.imbue ( std::locale("") );

usually inherit the locale from the environment, _except_ for the
character encoding as it appears.

However,

setlocale(LC_AL L, "");

does inherit even the code conversion from the environment on my
system.

Test case:

#include <iostream>
#include <cstdio>

using namespace std;

int main(int argc, char** argv)
{
cout.imbue( locale("") );

// 0xc3 0xa4 is UTF8 for 0xe4, which is ä

cout << " cout: " << char(0xc3) << char(0xa4) << endl; //prints ä
wcout << L"wcout: " << wchar_t(0xe4) << endl; //doesn't

setlocale(LC_AL L, "");

char const* narrow = "\xc3\xa4";
wchar_t const* wide = L"\xe4";

printf("printf narrow: %s\n", narrow); //prints ä
printf("printf wide: %ls\n", wide); //does likewise
}

I presume this is just my broken platform?
Nothing broken about it that I can see. The C++ Standard says regrettably
little about how all this stuff should work.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Sep 30 '06 #9

P.J. Plauger wrote:
"Jens Theisen" <jt***@arcor.de wrote in message
news:87******** ****@arcor.de.. .
"P.J. Plauger" <pj*@dinkumware .comwrites:
In practice they're not broken and you can write Unicode characters.
As with any other Standard C++ library, you need an appropriate
codecvt facet for the code conversion you favor. See our add-on
library, which includes a broad assortment.
This is probably a bit OT, but I'm wondering why you would have to set
this up manually. I don't know what the language requirements are
(there are probably none, as usual), but on unix platforms

std::cout.imbue ( std::locale("") );

usually inherit the locale from the environment, _except_ for the
character encoding as it appears.

However,

setlocale(LC_AL L, "");

does inherit even the code conversion from the environment on my
system.

Test case:
[snip]

I presume this is just my broken platform?

Nothing broken about it that I can see. The C++ Standard says regrettably
little about how all this stuff should work.
I've put my questions in another thread as promised at:

http://groups.google.com/group/comp....651dc92162b5fb

I'm not sure what the standard says about how wide character streams
should behave and questions about that are also in my post.

If the streams aren't broken then I'm sure you can tell me what I'm
missing to get them to work.
K

Oct 2 '06 #10

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

Similar topics

1
7370
by: Saurabh Aggrawal | last post by:
Hi, On line nos. 24, 25, 26 24: wstring cfMethods = {{L"setLabel"},{L""}}; 25: wstring cfProperties= {{L"isVisible"},{L""}}; 26: wstring cfEvents = {{L"onLoad"},{L"onQueryTerminate"},{L"onTerminate"},{L""}}; i am getting the following errors:
0
1760
by: Steven T. Hatton | last post by:
I copied what I believe is a complete representation of <iosfwd> from ISO/IEC 14882:2003(E). My understanding of the rules covering typedefs, templates and forward declarations is not very solid. As I understand what is presented here, the typedefs are declaring typedef-names which refer to incomplete types. That is to say, they are becoming synonyms for forward declarations of class template instances. I see nothing in the Standard...
2
8795
by: anubis | last post by:
Heay, i've got this problem: http://rafb.net/paste/results/lpNgbn49.html i'm using wifstream to read utf-16 file and i've got this problem, that each byte is read into seperate char while little-endian uses at least 2 bytes for one sign. the code of method is in the above letter, also with the problem i'm attaching below:
3
5850
by: uday.sen | last post by:
Hi, I am porting a piece of code from VC++ to linux platform. My compiler is g++ 3.2.2. I am getting following error: no matching function for call to A::setResponse(std::wstring) candidates are A::setResponse(std::wstring &) ---> This is indeed the signature I am using this function as:
8
16472
by: Divick | last post by:
Hi all, can somebody tell how much std::wstring is supported across different compilers on different platforms? AFAIK std::string is supported by almost all C++ compilers and almost all platforms, is that also the case with wstring? Another related question that I have is, is it advisable to use wstring than string for unicode support? To be able to support Unicode build, is it that all the occurrence of std::string will need to be...
9
7462
by: Mwob | last post by:
Hi, I have a very simple function, declared like this: wstring CMyClass::GetDate() { .... wcsftime (sDateString,255,L"%d-%b-%Y",&dTmpDate); wstring sText(sDateString); return sText;
2
12641
by: wolverine | last post by:
Hi Could any one tell me how to compile the code using wstring with g++ or gcc. I am using gcc version 3.4.2 #include<iostream> #include<string> using namespace std; int main() {
10
4859
by: v4vijayakumar | last post by:
1. why the following program is not working as expected? #include <iostream> using namespace std; int main() { string t("test"); wcout << (wchar_t *) t.c_str() << endl; wcout << t.c_str() << endl;
1
3808
by: v4vijayakumar | last post by:
1. Why the following program is not working as expected? #include <iostream> using namespace std; int main() { string t("test"); wcout << (wchar_t *) t.c_str() << endl; wcout << t.c_str() << endl;
0
8397
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8827
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8732
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8503
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8605
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6167
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
1957
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1620
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.