Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Using Visual Studio 2015 with VC++

char buffer[100];

CString strTmp = _T("");

strTmp.Format(_T("%s"), buffer); doesn't make no error message.

But after analysing the code, it makes warning "invald parameters transit position.....etc"

Is there any dangerous point of above code?

What I have tried:

In almost of my programs with the moving value from char buffer to CString variable,
I don't know what is error.
Posted
Updated 30-Jun-16 2:07am
v2

It is the old ANSI and UNICODE problem.

To avoid I use TCHAR, which brings the preprocessor to use the right char-type.

C++
TCHAR buffer[100];

CString strTmp = _T("");

strTmp.Format(_T("%s"), buffer); 


I guess that your compiler will find after this change some errors in accesing the buffer. And it is right: change that too!!!
 
Share this answer
 
In addition to solution 1:

If you need to format a string with a char* string argument, you can specify this in the format by using the h prefix:
char buffer[100] = "A char* string";
CString strTmp;
strTmp.Format(_T("%hs"), buffer)

This will convert the char* string to a wide string during runtime and insert that with Unicode builds. With non-Unicode builds the prefix is ignored.
 
Share this answer
 
Comments
KarstenK 1-Jul-16 3:36am    
Nice, but it makes the code harder to understand because it leads to mixing the string type. And only leads to bugs.
Jochen Arndt 1-Jul-16 3:46am    
Thank you.

My intention was not to suggest to do it this way when using a local instance of the buffer.

It is for the cases when having an existing char* string that must be formatted (e.g. passed as parameter to a function, retrieved from a library function call, or read from an ANSI/ASCII file, device, or socket).

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