Recent

Author Topic: TMemoryStream file format  (Read 2685 times)

jamie

  • Hero Member
  • *****
  • Posts: 6131
Re: TMemoryStream file format
« Reply #15 on: June 09, 2020, 01:29:48 pm »
If this is not school project then a short string should used write the record once as a whole.
The only true wisdom is knowing you know nothing

wp

  • Hero Member
  • *****
  • Posts: 11923
Re: TMemoryStream file format
« Reply #16 on: June 09, 2020, 01:30:15 pm »
Again (the last time): the string characters are NOT embedded in the record, the record contains only a pointer to the characters somewhere in memory. What you describe is an array of chars with terminating null. This is not a dynamic Pascal string as used here.
« Last Edit: June 09, 2020, 01:33:53 pm by wp »

jcmontherock

  • Full Member
  • ***
  • Posts: 236
Re: TMemoryStream file format
« Reply #17 on: June 09, 2020, 04:01:10 pm »
I give you the project.
Windows 11 UTF8-64 - Lazarus 3.2-64 - FPC 3.2.2

rvk

  • Hero Member
  • *****
  • Posts: 6171
Re: TMemoryStream file format
« Reply #18 on: June 09, 2020, 04:06:26 pm »
I give you the project.
And what is the problem?

Did you read the other posts? If you use AnsiString then your sName is NOT saved to the file.
We even showed you the calculation for it.

The solution was also given (use string[255] or ShortString, which isn't a pointer but a stringarray which will be saved).

wp

  • Hero Member
  • *****
  • Posts: 11923
Re: TMemoryStream file format
« Reply #19 on: June 09, 2020, 05:28:36 pm »
OK, maybe you do not understand what rvk and I were trying to explain. Therefore I fixed your demo to work. I marked all lines changed by my initials.

The code stores only the non-string elements of the record - this is easy when the string is moved to the end of the record definition, and the size of the string (the size of a pointer) is subtracted from the size of the record. Then the string is saved separately by calling ClearString.WriteAnsiString.

Reading is similar.

Alternatively you could declare the string sName as String[255] because this kind of string is NO pointer and has its characters inside the record. But because now even for an empty string 255 bytes are occupied in the file I prefer the other solution.

jcmontherock

  • Full Member
  • ***
  • Posts: 236
Re: TMemoryStream file format
« Reply #20 on: June 09, 2020, 05:42:52 pm »
OK, thenks a lot to everybody...
Windows 11 UTF8-64 - Lazarus 3.2-64 - FPC 3.2.2

 

TinyPortal © 2005-2018