Solved

char ptr to wstring - Urgent!

Posted on 2003-12-01
12
1,140 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
Independent Software Vendors: 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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

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 …
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

739 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