Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

array[1..x] of char to string? 1

Status
Not open for further replies.

autolab

Programmer
May 23, 2002
4
US
Please look at my code snippets below. I am reading data from a file created by an old DOS app. My code works, I can read the file. My question is, am I correct in using the array[] of char to store the data. If so, how can I get the arrays into strings so I can manipulate them and then get the strings back into the arrays before writing to the file. Is there some sort of ArrayToString() function or StrToArray() function? I understand that Delphi strings are arrays, but trying to assign the array to a string generates an error. Or is there an entirely better way to do this? I await enlightenment from Delphi gurus. Thank you.


type
TInven = packed record
Item : array[1..21] of char;
Group: array[1..15] of char;
Descr: array[1..21] of char;

procedure TForm1.SayEm;
var
Armor : File of TInven;
InvData : TInven;
begin
gGoRec := StrToInt(Edit1.Text);
if FileExists('ADIN1001.DAT') then
begin
AssignFile(Armor,'ADIN1001.DAT');
FileMode := 0; {read only}
Reset(Armor);
Seek(Armor,gGoRec);
Read(Armor,Invdata);
end;
with InvData do
begin
Label1.Caption := Item;
Label2.Caption := Group;
Label3.Caption := Descr;
etc...
 
As for the first part (copying array of char to string) :
Code:
var
  S : String;
  A : array ... of Char;
begin
  ...
  S := Copy(A, Low(A), High(A) - Low(A) + 1));
  ...
end;
And as for second part:
Code:
var
  S : String;
  A : array ... of Char;
  i : Ineger;
begin
  ...
  for i := Low(A) to High(A) do
   S[i - Low(A) + 1] := A[i];
  ...
end;
I am not sure if it's the best way to it but it should do the job.

--- markus
 
Err.. the second part should look like :
Code:
  for i := Low(A) to High(A) do
   A[i] := S[i - Low(A) + 1];

--- markus
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top