Board index » delphi » Problem writing 'record' structure

Problem writing 'record' structure

Hi,

I'm developing with Delphi 3.02 and I've turned up rather an odd problem
while trying to write Pascal 'records' to a TFileStream. I have to write a
number of different record layouts, hence the reason for doing it this way.
Each record's fields are mainly short strings, in the format 'String[n]',
with a few 'Char'.

Whether I write the structure directly, as in:-

FileStream1.Write(RecType1, SizeOf(RecType1));

... or concatenate all the fields into one long string and write that, the
problem is that the length byte of each short string appears in the output.

The problem appears to be using the Pascal 'record' structure - concatenate
fields declared outside one and there's no problem, use 'record' and there's
the problem.

I appreciate that one obvious answer is not to use 'record', but without
some sort of structure, uniquely identifying some fields, and documenting
the whole thing, becomes a bit of a nightmare.

As always, thanks in advance for any pointers, info., etc.

Regards,

Stuart

 

Re:Problem writing 'record' structure


Hi Stuart,

you might be able to solve your problem by defining your string[n] as
array[0..n-1] of char. You can treat these as PChars, and in the newer versions
of Delphi these are easily interchangable with the String type.

Alex.

Quote
Stuart wrote:
> I'm developing with Delphi 3.02 and I've turned up rather an odd problem
> while trying to write Pascal 'records' to a TFileStream. I have to write a
> number of different record layouts, hence the reason for doing it this way.
> Each record's fields are mainly short strings, in the format 'String[n]',
> with a few 'Char'.

> Whether I write the structure directly, as in:-

> FileStream1.Write(RecType1, SizeOf(RecType1));

> ... or concatenate all the fields into one long string and write that, the
> problem is that the length byte of each short string appears in the output.

> The problem appears to be using the Pascal 'record' structure - concatenate
> fields declared outside one and there's no problem, use 'record' and there's
> the problem.

> I appreciate that one obvious answer is not to use 'record', but without
> some sort of structure, uniquely identifying some fields, and documenting
> the whole thing, becomes a bit of a nightmare.

> As always, thanks in advance for any pointers, info., etc.

> Regards,

> Stuart

Re:Problem writing 'record' structure


Im Artikel <7bgk6t$sj...@nclient3-gui.server.{*word*269}.net>, "Stuart"
<stuart....@{*word*269}.net> schreibt:

Quote
>problem is that the length byte of each short string appears in the output.

That's correct, the length *must* occur in the output!

Quote
>FileStream1.Write(RecType1, SizeOf(RecType1));

The SizeOf function returns the static size of the data type, i.e. with all
strings as declared in the record structure. But if not all characteres in a
string actually are used, the count of valid characters must be part of the
stored data. Since the short strings by convention have the character count
stored in the [0] character, it's simply output as part of the string, ready
for use as the length indicator after reading the data back into memory.

Beware of dynamic strings, when writing records. Perhaps the compiler will tell
you that you attempt something impossible then, but I'm not sure. At least you
should know, that some data types, like dynamic strings or arrays, are stored
in separate memory, and the the record that contains such data items really
includes only a pointer to the data. Then, when you write the record, only the
value of the pointer is stored, but not the data, that it refers to. When such
a record is read back later, it's most likely that no meaningful data is stored
at the address, that the old pointer refers to.

Whenever you have some kind of reference in a record, you must save that
referenced information also with your data, and retrieve it when later reading
the data from the file.

DoDi

Other Threads