Solved

char ptr to wstring - Urgent!

Posted on 2003-12-01
12
1,139 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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 …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

749 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