This question already has answers here:
How to concatenate multiple CString
(2 answers)
Closed 8 years ago.
In my program, I need put a CString variable in a MessageBox. I use the following code:
messagebox("hi" + txt);
But I get the following error message:
error C2678: binary '+' : no operator found which takes a
left-hand operand of type 'const char [3]' (or there is no acceptable
conversion)
you can use a CString variable to format, then pass it to MessageBox.
_T() is a macro for Unicode or MBCS.
you should make sure txt is same encode as str,
CString str ;
str.Format(_T("Hi %s"), txt);
Use format method of the CString. The Format method do thing like how printf, sprintf works.
Example
CString str ;
str.Format("Hi %s", txt);
Use the _T macro to wrap the string literal into a CString:
messagebox(_T("hi") + txt);
Related
This question already has answers here:
C++ Convert string (or char*) to wstring (or wchar_t*)
(19 answers)
Closed 2 years ago.
To convert a string literal to wchar_t we can use L like:
wchar_t variable[10] = L"some text";`
But if the string is stored inside a variable then how do I convert it to wchar_t?
Suppose the string is in a variable
string varString="someText";
I want to store it in a variable of type wchar_t, for example wchar_t var;
How do I type cast and store it?
I want to place the variables inside a loop where their values will change with each cycle:
for(i=0;i<10;i++)
{
var=(*some kind of casting*)varString;
}
Two different ways...depending on simplicity... This is in Visual C++ group...
So first I would try using CStringW. Depending on #defines your regular CString might be a CStringA or CStringW. But, you can say CStringW.
CStringW sWide = "abcdef"; // uses current thread code page
const wchar_t* pWide = sWide.GetString(); // pointer only valid for scope of sWide
Or you can use MultiByteToWideChar() API.
wchar_t wszBuf[512];
MultiByteToWideChar(CP_ACP, 0, "abcdef", 6, wszBuf, _countof(wszBuf)); // substitute "abcdef" and the 6 (length) for your usage...
This question already has answers here:
vc++ - How to convert a CString into LPCWSTR
(6 answers)
Closed 6 years ago.
I have a function which retrieves resources from .rc file using CStringW.
I want use this returned value in sprintf_s.Is there any way?
//Snippet
sprintf_s(szMsgBoxText, LoadFromResource(IDS_INSTALLATION_COMPLETE), g_szProductName);
CStringW LoadFromResource(int ID)
{
CStringW m_resoucestring(MAKEINTRESOURCE(ID));
return m_resoucestring;
}
sprintf_s giving me an error.Is there any alternative for this?
Use CStringW::Format which does exactly the same as sprintf_s. So your code will become:
CStringW sText;
sText.Format(LoadFromResource(IDS_INSTALLATION_COMPLETE), g_szProductName);
or even
CStringW sText;
sText.Format(IDS_INSTALLATION_COMPLETE, g_szProductName);
You can pass CStringW directly to any function that accepts LPCWSTR type of parameter as it has corresponding cast operator.
This question already has answers here:
Convert int to LPCWSTR by using wsprintf
(1 answer)
Display a Variable in MessageBox c++
(5 answers)
Closed 8 years ago.
I have a proplem , like :
Ex :
MessageBoxW(0,L"Đây là ABC (This is ABC)",L"Lỗi (Error)",0);
All ok !
But i want to replace ABC to variable , like it :
char buff[500];
char author[] = "ABC";
sprintf_s(buff,"Đây là %s (This is %s)",author);
MessageBoxW(0, WHAT WILL BE HERE,L"Lỗi (Error)",0);
I hope someone may help !
You can certainly display a variable, but it has to be of the correct type. MessageBoxW takes a LPCWSTR (wide), and a char[] provides a LPCSTR (narrow) instead. So swap out the types accordingly:
WCHAR buff[500]; // WCHAR not char
WCHAR author[] = L"ABC"; // WCHAR not char
swprintf_s(buff, L"Đây là %s (This is %s)", author); // swprintf_s not sprintf_s
MessageBoxW(0, buff, L"Lỗi (Error)", 0);
It's also a good idea to avoid the raw buffers and use a wrapper class such as ATL::CStringW or std::wstring.
(I had some trouble deciding whether to answer this. The related question Why can't I display this string on MessageBox? seems like a duplicate, but it's closed as a duplicate of Cannot convert parameter from 'const char[20]' to 'LPCWSTR' which does not answer this question. In fact its answer is included in this question.)
This question already has answers here:
How to convert std::string to LPCSTR?
(9 answers)
Closed 9 years ago.
I tried this in c++:
std::string teststring = "hello";
MessageBox(NULL,teststring,NULL, NULL);
error C2664: 'MessageBoxA' : cannot convert parameter 2 from 'std::string' to 'LPCSTR'
First, it looks like Visual C++ so tag it properly.
You can get the inside buffer using c_str() method on a std::string, so your code becomes:
std::string teststring = "hello";
MessageBox(NULL,teststring.c_str(),NULL, NULL);
MessageBox's second and third parameter expect a C string.
To get a C string from a std::string you call c_str(), therefor the correct way to call it is:
std::string teststring = "hello";
MessageBox(NULL, teststring.c_str(), NULL, NULL);
what about try this?
std::string teststring = "hello";
LPCSTR tmp = teststring .c_str()
MessageBox(NULL,tmp ,NULL, NULL);
This question already has an answer here:
How to convert 'wchar_t *' to 'const char *'
(1 answer)
Closed 9 years ago.
I am a kind of new for c++ , while working on the windows CE .net compact application
while trying to write hexa datas to a file
CString dataBlock1;
dataBlock1 = "";
CString temp;
for(int i = 0; i < rLen; i++)
{
temp.Format(L"%02X ",rec[i]);
dataBlock1 += temp;
}
std::ofstream out(file);
I am getting this error can not convert parameter 1 from wchar * to const char*
on while using the below write function to write hexa datas to a file
out.write(myReader.dataBlock1.GetBuffer(),myReader.dataBlock1.GetLength());
how can we convert wchar_* to const char* to make the write function work.
Thanks.
You can use the wcstombs function, reference here.
Windows has a set of classes and functions that take wchar_t, which is text stored as UTF-16, and char, which is text stored in your ANSI character set. If you have pointer to wchar_t you either need to use an appropriate class or function that accepts wchar_t, or you need to convert the data to your ANSI character set.
In this case, you want the wchar_t variant of ofstream, wofstream.