Problem by using TFileStream:   Save and Read ShortString in Delphi2010

wantime
wantime used Ask the Experts™
on
hello all,

i have tried to save 'ABCDEFGHIJ' as ShortString in a file, when i read it from the file , i get
'ABCDEFGHIJ\ö'. It seems there are more sysbols come out.

following are my codes, how should i change it to make it work?

any tips are appreciated.

thanks,

wantime
-------------------------------------------------------------
--------------------------------------------------------------
procedure TForm1.LoadShortStingClick(Sender: TObject);
var
  ID: String[10];
begin
  aStream := TFileStream.Create(mpath, fmOpenRead);
  aStream.Read(ID[1], 10);
  Edit2.text := ID;
  aStream.Free;
end;

procedure TForm1.SaveShortStingClick(Sender: TObject);
var
  S: String[10];
begin

  S[1] := 'A';
  S[2] := 'B';
  S[3] := 'C';
  S[4] := 'D';
  S[5] := 'E';
  S[6] := 'F';
  S[7] := 'G';
  S[8] := 'H';
  S[9] := 'I';
  S[10] := 'J';

  aStream := TFileStream.Create(mpath, fmCreate);
  aStream.Write(S[1], 10);

  aStream.Free;
end;
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
MerijnBSr. Software Engineer

Commented:
You need unicode? Did you try using AnsiString instead of String?

Author

Commented:
RE: MerjnB

i just want to know how ShortString works and why my codes doesn't work...  

Sr. Software Engineer
Commented:
In Delphi2009 and newer (so yours too), string is no longer 1 byte per char, but multiple bytes.

So, while
var
  ID:  String[10];

Looks like an array of 10 bytes, it's not.

So if you do
   aStream.Read(ID[1], 10);

You read 10 bytes into an array which is longer then 10 bytes. Result is that you don't know what is in the rest of the array. There is probably some crap in there which is interpreted as a string when you try to put it in the edit.

Author

Commented:
RE MerijinB:

so, how should i change the codes?

if i change   aStream.Read(ID[1], 10); to   aStream.Read(ID[1], 9);
i can get the right result, also, just 9 symbol ''ABCDEFGHI', no more \ö' at the end, but  'J' is also not attached.

Author

Commented:
and i also think Shortstring actually declarations allocate a buffer for a specific number of AnsiChars. The total Bytes for  ID is 11. (which was defined as   ID:  String[10];)

but where is the problem?
MerijnBSr. Software Engineer

Commented:
No unicode Delphi here atm, but you can try
MerijnBSr. Software Engineer

Commented:
where did my code snippet go?

procedure  TForm1.LoadShortStingClick(Sender: TObject);
var
  ID:  AnsiString[10];
begin
  aStream := TFileStream.Create(mpath,  fmOpenRead);
  aStream.Read(ID[1], 10);
  Edit2.text := ID;
   aStream.Free;
end;

procedure TForm1.SaveShortStingClick(Sender:  TObject);
var
  S: AnsiString[10];
begin

  S[1] := 'A';
   S[2] := 'B';
  S[3] := 'C';
  S[4] := 'D';
  S[5] := 'E';
   S[6] := 'F';
  S[7] := 'G';
  S[8] := 'H';
  S[9] := 'I';
   S[10] := 'J';

  aStream := TFileStream.Create(mpath, fmCreate);
   aStream.Write(S[1], 10);

  aStream.Free;
end;

Author

Commented:
it's not possible to define S as AnsiString[10].


Author

Commented:
@MerijnB

it works, if i use

   for I := 1 to 10 do begin
       newId := newId + Id[I];
    end;

in  TForm1.LoadShortStingClick and then output newId, where newId has type String[10].

do you know what is the different between newId and Id? why newId has correct value 'ABCDEFGHIJ', but Id has more symbols at the end of string, just like 'ABCDEFGHIJ\ö'

Author

Commented:
if i don't use shortstring such as String[10] and choose to use unicode string. how can i save 'ABCDEFGHIJ' and then read them, i mean, just write 10 chars in a file and then read 10 chars out?
MerijnBSr. Software Engineer

Commented:
wow, I've missed some posts I see :)

Good you found it! You need anything more from me on the questions I missed?

Author

Commented:
thanks. not yet.

i will come again when i get new problem.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial