ostringstream Unicode problem

A

Angus

Sorry about the platform specific code here, but I include to show what I am
doing. I am trying to create a char* directory listing.

If I compile the code below as Unicode then I get memory addresses in
strResponse. But it works fine if not compiled with Unicode. How would I
edit the code to work under Unicode?

WIN32_FIND_DATA stData;
//do first find call
HANDLE hReturn = ::FindFirstFile(TEXT("C:\\*.*"), &stData);
if (hReturn == INVALID_HANDLE_VALUE) return -1;

std::eek:stringstream ss;
ss << stData.cFileName << "\r\n";

while :):FindNextFile( hReturn, &stData))
{
ss << stData.cFileName << "\r\n";
}

FindClose(hReturn);
std::string strResponse = ss.str();

stData.cFileName is a char buffer.

Angus
 
V

Victor Bazarov

Angus said:
Sorry about the platform specific code here, but I include to show
what I am doing. I am trying to create a char* directory listing.

If I compile the code below as Unicode then I get memory addresses in
strResponse. But it works fine if not compiled with Unicode. How
would I edit the code to work under Unicode?

WIN32_FIND_DATA stData;
//do first find call
HANDLE hReturn = ::FindFirstFile(TEXT("C:\\*.*"), &stData);
if (hReturn == INVALID_HANDLE_VALUE) return -1;

std::eek:stringstream ss;
ss << stData.cFileName << "\r\n";

while :):FindNextFile( hReturn, &stData))
{
ss << stData.cFileName << "\r\n";
}

FindClose(hReturn);
std::string strResponse = ss.str();

stData.cFileName is a char buffer.

You probably need to post this question to a Microsoft newsgroup.
There is no such thing as "compilation as Unicode" defined in C++
language Standard. I believe it's compiler- and platform-specific.
Check out FAQ section 5 for the list of the suggested newsgroups.

V
 
B

BobR

Victor Bazarov wrote in message... [snipped]
[ For OP (just an idea, I don't know) ]
On the C++ side:

A std::eek:stringstream 'defaults' to 'char'.
It will need to be passed the parameters of the Unicode for it to be used.

The 'footprint' is (from <sstream>):

template <typename _CharT, typename _Traits, typename _Alloc>
class basic_ostringstream : public basic_ostream<_CharT, _Traits>
{ /* .... */ };

Fill in the [ ]s, at bare minimum fill in the first one:
std::eek:stringstream< [Unicode char], [traits], [allocator] > ss;

If you don't know and can't find the info, you might try:

std::eek:stringstream< wchar_t > ss;

[ Thanks for the use of your post, Victor. ]
 
B

BobR

BobR wrote in message...

Just checked the wxWidgets docs. They say Unicode is two-bytes wide (wchar_t
type), so, the following line should be the one used.

std::eek:stringstream< wchar_t > ss;

Please let us know if it works.

std::cout<<" sizeof(wchar_t) ="<<sizeof(wchar_t)<<std::endl;
// out: sizeof(wchar_t) =2
 
V

Victor Bazarov

BobR said:
BobR wrote in message...

Just checked the wxWidgets docs. They say Unicode is two-bytes wide
(wchar_t type), so, the following line should be the one used.

std::eek:stringstream< wchar_t > ss;

Please let us know if it works.

That's

std::basic_ostringstream<wchar_t> ss;

or

std::wostringstream ss;
std::cout<<" sizeof(wchar_t) ="<<sizeof(wchar_t)<<std::endl;
// out: sizeof(wchar_t) =2

V
 
?

=?iso-8859-1?q?Kirit_S=E6lensminde?=

Sorry about the platform specific code here, but I include to show what I am
doing. I am trying to create a char* directory listing.

If I compile the code below as Unicode then I get memory addresses in
strResponse. But it works fine if not compiled with Unicode. How would I
edit the code to work under Unicode?

WIN32_FIND_DATA stData;
//do first find call
HANDLE hReturn = ::FindFirstFile(TEXT("C:\\*.*"), &stData);
if (hReturn == INVALID_HANDLE_VALUE) return -1;

Microsoft pushes the idea that _T and and their TEXT macros is all the
difference that you need for Windows Unicode and Windows narrow
character support. Unfortunately it isn't true (although it was more
nearly so before they started to use the standard C++ libraries).
std::eek:stringstream ss;

std::eek:stringstream is for char. If you want to compiler for both
Unicode and narrow characters then you'll need to use conditional
compilation to determine the correct type. For wchar_t this needs to
be std::wostringstream.
ss << stData.cFileName << "\r\n";

You will also need to use the TEXT or _T macros here (can't remember
which does what as I don't use them I'm afraid).

ss << ... << _T( "\r\n" );
while :):FindNextFile( hReturn, &stData))
{
ss << stData.cFileName << "\r\n";

}

FindClose(hReturn);
std::string strResponse = ss.str();

stData.cFileName is a char buffer.

This will need to change between char and wchar_t depending on how
you're compiling it.

This is all very fidlly. Why are you even bothering with narrow
character support? On Windows it seems pointless these days. If you're
going to the bother of supporting Unicode compilation then just use
that throughout and forget about narrow character support.

You can still use narrow characters where you need to interact with
legacy systems and to handle formats that are specified as 7/8 bit
characters.


K
 
B

BobR

Victor Bazarov said:
That's

std::basic_ostringstream<wchar_t> ss;

or

std::wostringstream ss;

Pick one:
a) Doooh, I can't believe I did that.
b) Duh, my brain went on strike.
c) all of the above.

Thanks for the correction, Victor.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top