By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,510 Members | 1,815 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,510 IT Pros & Developers. It's quick & easy.

ostringstream Unicode problem

P: n/a
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::ostringstream 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
Jun 18 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Angus wrote:
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::ostringstream 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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 18 '07 #2

P: n/a

Victor Bazarov wrote in message... [snipped]
Angus wrote:
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?

std::ostringstream ss;
[ For OP (just an idea, I don't know) ]
On the C++ side:

A std::ostringstream '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::ostringstream< [Unicode char], [traits], [allocator] ss;

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

std::ostringstream< wchar_t ss;

[ Thanks for the use of your post, Victor. ]
--
Bob R
POVrookie
Jun 19 '07 #3

P: n/a

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::ostringstream< 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
--
Bob R
POVrookie
Jun 19 '07 #4

P: n/a
BobR wrote:
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::ostringstream< wchar_t ss;

Please let us know if it works.
That's

std::basic_ostringstream<wchar_tss;

or

std::wostringstream ss;
>
std::cout<<" sizeof(wchar_t) ="<<sizeof(wchar_t)<<std::endl;
// out: sizeof(wchar_t) =2
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 19 '07 #5

P: n/a
On Jun 19, 4:16 am, "Angus" <nos...@gmail.comwrote:
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::ostringstream ss;
std::ostringstream 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

Jun 19 '07 #6

P: n/a

Victor Bazarov <v.********@comAcast.netwrote in message...
BobR wrote:
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::ostringstream< wchar_t ss;

Please let us know if it works.

That's

std::basic_ostringstream<wchar_tss;

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.

--
Bob R
POVrookie
Jun 19 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.