I have a string that contains null characters.
I’ve tried to save it to a file with this code:
myStringList.Text := myString;
myStringList.SaveToFile('c:\myfile');
Unfortunately myStringList.Text is empty if the source string has a null character at the beginning.
I thought only C string were terminated by a null character, and Delphi was always fine with it.
How to save the content of the string to a file?
I think you mean “save a string that has
#0characters in it”.If that’s the case, don’t try and put it in a
TStringList. In fact, don’t try to save it as a string at all; just like in C, a NULL character (#0in Delphi) causes the string to be truncated at times. Use aTFileStreamand write it directly as byte content:To read it back: