简体   繁体   中英

Printf used in unfamiliar fashion

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

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.

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.

你可能想要wsprintf ...看起来像是一个带有名为printf的成员函数的类,它可能只是将其参数传递给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; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM