views:

150

answers:

4

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*.

A: 

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.

Ben Voigt
`printf("%d",mStreamLength)` would be printing out mStreamLength as an integer
John Knoeller
@John: no, it's printf the member function of some class (we are only told it is part of C++ Builder), not printf the C library function. So it could do anything, and probably formats into a string, not actually "printing" anything. The standard function that does the same, using a wchar_t* which is what the poster asked for, is wsprintf.
Ben Voigt
+1  A: 

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.

gbrandt
+3  A: 

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.

David Dean - Embarcadero
A: 

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; 
Remy Lebeau - TeamB