473,378 Members | 1,469 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 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 14283
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

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

Similar topics

4
by: sorty | last post by:
Hi, I have read in many places that TCHAR can be 'char' or 'wchar_t' depending on ANSI or UNICODE. I have also read that LPTSTR is a long pointer to a TCHAR. I am confused about the...
9
by: alex | last post by:
how do i initialize an LPTSTR variable? i'm trying to call a microsoft function LONG QueryStringValue( LPCTSTR pszValueName, LPTSTR pszValue, ULONG* pnChars ) throw( );pszValue is supposed...
3
by: free2cric | last post by:
Hi , is this correct to have final LPTSTR to char * CString fname(argv ); LPTSTR p1 = fname.GetBuffer(fname.GetLength()); char teststr; sprintf(teststr,"%s",p1); //or strcpy(teststr,p1); ...
1
by: Steven Blair | last post by:
Hi there. Having a problem calling a win32 function from a dll. Here is the function header: DWORD thrdGetHcNum (DWORD nPort, DWORD nBaudrate, LPTSTR lpszHcNum) //Description of the 3rd...
0
by: Nils M. Lunde | last post by:
Hi! I have an unmanaged function: BOOL WINAPI UnmanagedClass::Function(LPTSTR sz1, LPTSTR sz2, LPTSTR sz3, HANDLE& hToken) . This I want to call from managed code. In managed code I have a...
1
by: Lewap | last post by:
Hi! I've piece of code like follow: <code> LPTSTR lpszPipename = (LPTSTR) "\\\\.\\pipe\\testpipe"; hPipe = CreateNamedPipe(
5
by: Sonu | last post by:
Hello everyone and thanks in advance. I have a multilingual application which has been built in MFC VC++ 6.0 (non-Unicode). It support English German Hungarian so far, which has been fine. But...
9
by: Hugo Amselschlag | last post by:
Hi there, I've implemented a local system hook to suppress certain windows beeing displayed by the axWebbrowser control. Now I need some more information before I can decide, whether to suppress...
2
by: sudhivns | last post by:
Hi, How to convert LPTSTR to char*.? Thanks in advance. Bye
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.