Solved

char ptr to wstring - Urgent!

Posted on 2003-12-01
12
1,141 Views
Last Modified: 2013-12-14
Hi all,

the following code doesn't worK:

      wchar_t *WcString = new wchar_t[20];
      
      size_t Number = sizeof(m_pcRegName);
      size_t NumberAffected = mbstowcs( WcString, m_pcRegName, Number );
      wstring wsFilePath = WcString;

It will compile, but WcString won't have the desired value.  m_pcRegName points to "C:\", which is translated into "???????..." by the mbstowcs function. What am I doing wrong? Is there any easy way of doing this conversion?

cheers,

henrik
0
Comment
Question by:henrikatwork
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 3
12 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 9850679
I'd make it read

    wchar_t *WcString = new wchar_t[strlen(m_pcRegName) + 1];
   
     size_t Number = strlen(m_pcRegName); // <---------------------!!!!!
    size_t NumberAffected = mbstowcs( WcString, m_pcRegName, Number );
    wstring wsFilePath = WcString;

because 'sizeof(char*)' == 4;
0
 
LVL 1

Author Comment

by:henrikatwork
ID: 9850814
Thanks,

it works much better, BUT my WcString has a bunch of question marks in it!! (That are not in the m_pcRegName)

How come??

cheers,

henrik
0
 
LVL 86

Expert Comment

by:jkr
ID: 9850836
How are you trying to display/use the wide string?

wcout << wsFilePath.c_str();

should yield a correct result.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 1

Author Comment

by:henrikatwork
ID: 9850912
I'm doing an append:


      wsFileName.append( wsFilePath );

wsFileName.append( "myFile.file" );

which turns out to "C:\???myFile.file"
:-(((

0
 
LVL 1

Author Comment

by:henrikatwork
ID: 9850924
I've tried using C: (without the backslash) , but then I get 4 '?' (????) instead of 3
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 9850947
mbstowcs assumes that m_pcRegName points to a multi-byte string.

Try:
--------8<--------
#include <iostream>
#include <string>

int main()
{
      const char m_pcRegName[] = "Hello, World";
      std::wstring wstr(m_pcRegName,m_pcRegName+strlen(m_pcRegName));
      std::wcout << wstr << L'\n';
}
--------8<--------

This assumes that m_pcRegName points to an ordinary narrow character string.

Worth a try, perhaps.
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 9850986
Should you be doing...

  wsFileName.append(L"myFile.file");
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 9850993
Hmm, try

   size_t Number = strlen(m_pcRegName) + 1;
   wchar_t *WcString = new wchar_t[Number];
   size_t NumberAffected = mbstowcs( WcString, m_pcRegName, Number );

   wcout << WcString << endl;

   wstring wsFilePath = WcString;

   wcout << wsFilePath.c_str();

If it still does not work, give


   size_t Number = strlen(m_pcRegName) + 1;
   wchar_t *WcString = new wchar_t[Number];
   wsprintfW ( WcString, "%S", m_pcRegName);

a try.

0
 
LVL 1

Author Comment

by:henrikatwork
ID: 9851017
Sorry,

but please post stuff that compile.

cheers,

henrik


0
 
LVL 86

Expert Comment

by:jkr
ID: 9852746
>>but please post stuff that compile.

???
0
 
LVL 1

Author Comment

by:henrikatwork
ID: 9857013
sorry, that comment was for rstavely, after having posted the code

nt main()
{
     const char m_pcRegName[] = "Hello, World";
     std::wstring wstr(m_pcRegName,m_pcRegName+strlen(m_pcRegName));
     std::wcout << wstr << L'\n';
}
--------8<--------

which isn't even close to correct...

Thanks for your help jkr!!
henrik
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 9857232
The excerpt compiles and executes on GCC3.2 and VC7.1, if you cut and copy between the indicated scissor marks, but I'm glad jkr was able to get you sorted out OK. My line of thought was that m_pcRegName might really not have been a multi-byte string (the wstr constructor is an iterator constructor), but clearly I was wrong, wrong, wrong...
0

Featured Post

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question