Question

I found this line of code when upgrading a C++ Builder project to RAD Studio 2009:

mProcessLength->Text.printf("%d",mStreamLength);

It doesn't compile in 2009, however what is the intent of this line and what is a better equivalent? Given that mProcessLength->Text is now a wchar_t*.

Was it helpful?

Solution

I suspect that you are getting these errors:

E2034 Cannot convert 'const char *' to 'const wchar_t *'
E2342 Type mismatch in parameter 'format' (wanted 'const wchar_t *', got 'const char *')

It's the parameters you are passing to printf that are mismatched. Changing it to:

mProcessLength->Text.printf(L"%d",mStreamLength);

will change your string literal to the correct type.

OTHER TIPS

Chances are good that wchar is handled as an UnicodeString VCL string type. It has a printf function that takes standard printf arguments except for the pointer to string. The UnicodeString itself is filled with the formatted string.

UnicodeString printf

So a UnicodeString is created on the stack automatically and the printf method is called, the pointer is then stuffed back into wchar.

You probably want wsprintf... looks like that was originally some class with a member function named printf, that probably just passed its parameters to wvsprintf.

On a side note, assuming Text is a property, then calling printf() on it will NOT upate the property with the new value. Both AnsiString and UnicodeString have constructors for formatting numeric values, so the following can be used instead, in all versions of C++Builder equally:

mProcessLength->Text = mStreamLength; 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top