471,109 Members | 1,511 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,109 software developers and data experts.

Issue while converting LPTSTR to char *

Hi All,

I am trying to convert LPTSTR to char* by using sprintf,
But sprintf copies only the first character from the string. i tried both array and pointer also , but the result is same. Here is my code...

LPTSTR TestStr= _T("Test String");
char s1[1000];
char *s2;
s2 = (char *) malloc(1000*sizeof(char));
sprintf(s1,"%s",TestStr);
sprintf(s2,"%s",TestStr);

Both s1 and s2 stores only 'T'.

Kindly help me in this regard...
Thanks for your time...

Regards
PSKumaran.
Nov 23 '07 #1
6 13690
AHMEDYO
112 100+
HI...

you using unicode or muli-byte characters??

Best Regards
Nov 23 '07 #2
Hi ,

Thanks for the reply.

I use UNICODE.

Regards
PSKumaran.
Nov 23 '07 #3
xoinki
110 100+
hi,
If u have compiled your program with UNICODE and _UNICODE defined, then LPTSTR will automatically become LPWSTR (Long pointer to a wide string) u cant assign this to char* but u can assign this to a variable of type wchar_t*.

Thnx
Xoinki
Nov 23 '07 #4
xoinki
110 100+
hi,
after using wchar_t* use swprintf() to get proper output.

Thnx
Xoinki
Nov 23 '07 #5
Thanks a lot Xoinki for the timely help.

I used wchar_t* as you suggested, It works fine.

Regards
PSKumaran.
Nov 23 '07 #6
weaknessforcats
9,208 Expert Mod 8TB
LPTSTR TestStr= _T("Test String");
char s1[1000];
char *s2;
s2 = (char *) malloc(1000*sizeof(char));
sprintf(s1,"%s",TestStr);
sprintf(s2,"%s",TestStr);
You could have avoided this whole mess by not using char in the first place. The whole point of the TCHAR mappings is to seamlessly switch between char and wchar_t based on the project settings.

That means once you use a TCHAR macro you cannot use char or wchar_t in your code. You fixed this by using wchar_t and now your code won't work with char.

You should have used TCHAR, _stprintf() and TEXT:
Expand|Select|Wrap|Line Numbers
  1. LPTSTR TestStr= TEXT("Test String");
  2. TCHAR s1[1000];
  3. PTCHAR s2;
  4. s2 = (PTCHAR) malloc(1000*sizeof(TCHAR));
  5. _stprintf(s1,TEXT("%s"),TestStr);
  6. _stprintf(s2,TEXT("%s"),TestStr);
  7.  
Read this http://msdn2.microsoft.com/en-us/library/ms860358.aspx.
Nov 24 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by sorty | last post: by
9 posts views Thread by alex | last post: by
3 posts views Thread by free2cric | last post: by
1 post views Thread by Steven Blair | last post: by
reply views Thread by Nils M. Lunde | last post: by
5 posts views Thread by Sonu | last post: by
9 posts views Thread by Hugo Amselschlag | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.