Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
2.33/5 (2 votes)
See more:
When I try to compile below code under VS2012, can NOT success.
Error message is identifier "LtmpStr" is undefined, on last line.
C++
int point_num;
TCHAR szText;
char tmpStr[] = {'\0'};
point_num = 100;
sprintf(tmpStr, "%d", point_num);
szText = _T(tmpStr);


What I have tried:

I have try change szText to *szText, still can not work.
Posted
Updated 15-Dec-17 1:31am
Comments
Richard MacCutchan 14-Dec-17 11:24am    
_T() is a macro used to generate string constants in either ANSI or Unicode. It is not a conversion function, so it will not work on character variables or arrays.

Take a look: c++ - How to convert char* to TCHAR[ ]? - Stack Overflow[^]

If you include the header file:

C++
#include "atlstr.h"

Then you can use the A2T macro as below:

// You'd need this line if using earlier versions of ATL/Visual Studio
// USES_CONVERSION;

char*  stheParameterFileName = argv[1];
TCHAR szName [512];
_tcscpy(szName, A2T(stheParameterFileName));
MessageBox(NULL, szName, szName, MB_OK);
 
Share this answer
 
Comments
flighta 15-Dec-17 7:00am    
Thank you, but still can not work.
Quote:
char tmpStr[] = {'\0'};
point_num = 100;
sprintf(tmpStr, "%d", point_num);
That's the road to the disaster: your buffer is 1-character long, sprintf overruns it.

Use instead the C++ features, try, for instance:
C++
std::basic_ostringstream<TCHAR> its;
its << point_num;
std::basic_string<TCHAR> ts = its.str();
MessageBox(NULL,ts.c_str(),_T("INFO"), MB_ICONINFORMATION);
 
Share this answer
 
Comments
flighta 15-Dec-17 7:07am    
Thank you.
I have change the code as below, but now have 3 error message.

#include "stdafx.h"
#include "atlstr.h"

TCHAR szText[10];
char tmpStr[10] = {'\0'};
int tmp_point_num = 10;
sprintf(tmpStr, "%d", tmp_point_num);
_tcscpy(szText, A2T(tmpStr));


error message:
error C2065: '_lpa': undeclared identifier
error C2065: '_convert': undeclared identifier
error C2065: '_acp': underclared identifier

Do I include more .h file???
CPallini 15-Dec-17 7:38am    
You have to specify
USES_CONVERSION
see
https://msdn.microsoft.com/en-us/library/87zae4a3.aspx
(look at the bottom of the page)
Finally I found the solution, thanks all!

C++
TCHAR szText[10]; 
	char tmpStr[10] = {'\0'};
	int tmp_point_num = 10;
	sprintf(tmpStr, "%d", tmp_point_num);
	size_t newsize = strlen(tmpStr) + 1;

	wchar_t * wcstring = new wchar_t[newsize];

	size_t convertedChars = 0;
	mbstowcs_s(&convertedChars, wcstring, newsize, tmpStr, _TRUNCATE);


of course, we need include

C++
<pre>#include <string>

#include "atlbase.h"
#include "atlstr.h"
#include "comutil.h"
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900