I'm pretty sure this worked in an earlier version of C++Builder
Maybe, but only in pre-CB2009 versions, where the Text
was an AnsiString
(since CB2009, it is now a UnicodeString
), and only as a fluke of undefined behavior, since you can't pass an (Ansi|Unicode)String
object to fprintf()
to begin with, it expects a pointer to a C-style character string instead. The sole data member of an AnsiString
is a char*
, and the sole data member of a UnicodeString
is a wchar_t*
/char16_t*
on Windows/Posix (respectively).
now I need to figure out the easiest way to get this to work in C++Builder 10.1.
You have a few choices:
cast the Text
to an AnsiString
and then use its c_str()
method to get a char*
pointer:
fprintf(out, "%s\n", AnsiString(Edit1->Text).c_str());
use fprintf()
with the %Ls
placeholder instead:
fprintf(out, "%ls\n", Edit1->Text.c_str());
use fwprintf()
instead:
fwprintf(out, L"%s\n", Edit1->Text.c_str());
use a more modern file I/O library, like the standard C++ std::ofstream
, or the RTL's TStreaWriter
, etc.
I have no idea why, but right now it seems to be writing only the first character of the text in Edit1 to the file.
Because you are telling fprintf()
(via %s
) to expect an 8bit null-terminated char*
string, but you are passing it a 16bit wchar_t*
string instead. ASCII characters have 0x00
bytes in them in 16bit strings.
Also if anyone has a link to a good source for best practices for dealing with strings C++Builder 10.1, I would be grateful.
Have you read Embarcadero's Migration and Upgrade documentation yet? It has a section on migrating ANSI code to Unicode.