 |
|
 |
well, actually you can do something like that:
std::string((CT2CA)path).c_str();
Best regards,
Damian Walczak
|
|
|
|
 |
|
 |
In MFC I use the following:
char szString[40] // - max size of struct component in my code
CString strCString("Some CString");
sprintf(szString, "%S", strCString) // note the capital 'S' conversion factor from unicode
There you go, conversion from CString to char* (or more acurately char[]) in one line...
__
Marc Dominic
|
|
|
|
 |
|
 |
Uhmmm... Either I don't clearly understand what are you trying to do or else..
CString has a LPCTSTR casting operator, which allows you to convert it to char* or WCHAR* (for unicode), depending on build settings. In case you want to copy the string into another char/WCHAR buffer, you can use _tcscpy/_tcsncpy or simple _tcsdup.
From the code you have posted I see that you are using a function that "Converts a sequence of wide characters to a corresponding sequence of multibyte characters" (from MSDN[^]) and its not exactly supposed to work, unless it is more sophisticated and knows to handle a regular strings as well (in non-unicode builds)
If you have tried in Unicode build to copy your string into a buffer of char, yes you would get the warning and it means exactly what it supposed to - you trying to push a 2-byte Unicode character into 1-byte regular (char). Better to listen to warnings sometimes
Philip Patrick
Web-site: www.stpworks.com
"Two beer or not two beer?" Shakesbeer
-- modified at 17:04 Wednesday 12th October, 2005
|
|
|
|
 |
|
 |
Dear Philip
Thanks for the comment.
I make use of the(LPCTSTR) when writing to the file, however when I apply this to fopen_s I get the following error message 'fopen_s' : cannot convert parameter 2 from 'LPCTSTR' to 'const char *'
That was why I came to the solution posted. As mentioned in the introduction it was for file name creation. I understand the warning message and as far as I could tell this was a cleaner method of pushing a 2-byte Unicode character into a regular char.
I appreciate the criticism as this how one learns. Any other tips are welcome.
Kindest Regards
Quintin Immelman
|
|
|
|
 |
|
 |
Are you using unicode builds? In case of unicode build, CString's operator LPCTSTR will cast the string into 2-byte (unicode) string, so calling fopen_s function will fail, since it cannot convert a 2-byte string into 1-byte. In this case you should use _tfopen_s function. Otherwise I don't see why this casting should fail. Maybe some "CRT security enhancements" caused this, gonna read a bit about that
Philip Patrick
Web-site: www.stpworks.com
"Two beer or not two beer?" Shakesbeer
|
|
|
|
 |
|
 |
He actually used a function called wcstombs_s that takes 5 parameters, not wcstombs that takes 3 parameters. I did a search in MSDN for that function as I have never seen it before, and the search came back as "Not Found". If the article author could provide a link to that function so I know what it does would be helpful. As for the rest I agree with your post. CString has GetBuffer() and operator LPCTSTR, and there is WideCharToMultiByte() and MultiBytetoWideChar() for conversions to or from unicode.
"You're obviously a superstar." - Christian Graus about me - 12 Feb '03
"Obviously ??? You're definitely a superstar!!!" - mYkel - 21 Jun '04
"There's not enough blatant self-congratulatory backslapping in the world today..." - HumblePie - 21 Jun '05
Within you lies the power for good - Use it!
|
|
|
|
 |
|
 |
Well, basically its the same function, just MSDN claims it to be more secure or whateva Never dig too deep into those. The link to this function below:
wcstombs_s on MSDN2[^]
Philip Patrick
Web-site: www.stpworks.com
"Two beer or not two beer?" Shakesbeer
|
|
|
|
 |
|
 |
Thanks Philip
Yes I am using Unicode... It is the default setting for my MFC config.
At least this article created a stir and I hope has introduced a new function for some people.
All in the name of learning, Or so I convince myself.
Thanks for the _tfopen_s, I'll try it now in a Project I'm working on.
Kindest Regards
Quintin Immelman
|
|
|
|
 |
|
 |
Yep and for me too, had to go and read something new, never was aware of that new set of CRT functions
Philip Patrick
Web-site: www.stpworks.com
"Two beer or not two beer?" Shakesbeer
|
|
|
|
 |
|
 |
Oh Crap
Now I have to search MSDN and MSDN2 to find anything. Why couldn't MS just leave everything in MSDN? Why create a new domain?
"You're obviously a superstar." - Christian Graus about me - 12 Feb '03
"Obviously ??? You're definitely a superstar!!!" - mYkel - 21 Jun '04
"There's not enough blatant self-congratulatory backslapping in the world today..." - HumblePie - 21 Jun '05
Within you lies the power for good - Use it!
|
|
|
|
 |
|
 |
Something tells me that I know the feeling :P
I personally search MSDN2 on rare ocassions, its working too slow to be taken seriously (at least for me)
Philip Patrick
Web-site: www.stpworks.com
"Two beer or not two beer?" Shakesbeer
|
|
|
|
 |
|
 |
Isn't MSDN2 meant for VS 2005 stuff and will be moved to MSDN when the beta is killed?
the secure string CRT functions with the "_s" appendix are a VS 2005 feature if I understand it correctly.
--
The Blog: Bits and Pieces
|
|
|
|
 |
|
 |
Johann Gerell wrote: Isn't MSDN2 meant for VS 2005 stuff and will be moved to MSDN when the beta is killed?
Ok, that makes sense. Thanks for the info
"You're obviously a superstar." - Christian Graus about me - 12 Feb '03
"Obviously ??? You're definitely a superstar!!!" - mYkel - 21 Jun '04
"There's not enough blatant self-congratulatory backslapping in the world today..." - HumblePie - 21 Jun '05
Within you lies the power for good - Use it!
|
|
|
|
 |