Question

I have some code that uses ReadStr and WriteStr for what I presume is writing a string to a binary file.

The explanation for WriteStr in the documentation states that it will write raw data in the shape of an AnsiString to the object's stream, which makes sense. But then ReadStr says that it reads a character. So are they not the opposite of each other?

Let say I have,

pName: String[80];

and I use WriteStr on it, what does it actually write? Since WriteStr expects AnsiString, does it cast pName to be such? In that case, does it not write the "Length" field into the stream because an AnsiString pointer points to the first element and not the length field? I was also looking and it seems String == AnsiString these days, but my question about the length field still remains the same.

If lets say it doesn't write the Length field into the file, does it still write the NULL at the end of the data? As such, can I find where the string ends by looking for a '\0'? Does ReadStr read until the NULL character?

Thank you kindly :)

Was it helpful?

Solution

In your pre-Unicode version of Delphi, WriteStr and ReadStr write and read an AnsiString value. The writing code writes the length, and then the string content. The reading code reads the length, allocates the string, and then fills it with the content.

This has the potential of involving a truncation when you assign the result of ReadStr to your 80 character short string.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top