I'll try to be the shortest possible. I have these DATA TYPES in my program.'
type
pha = (SenoraBlanco, SenorVerde, SenoraCeleste, ProfesorCiruela,
SenoritaEscarlata, CoronelMostaza, Biblioteca, Cocina, Comedor, Estudio,
Vestibulo, Salon, Invernadero, SalaDeBaile, SalaDeBillar, Candelabro,
Cuchillo, Cuerda, LlaveInglesa, Revolver, Tubo);
a = Candelabro..Tubo;
h = Biblioteca..SalaDeBillar;
p = SenoraBlanco..CoronelMostaza;
sbr = record
arma: a;
habt: h;
prj: p;
end;
var
game: text;
sobre: sbr;
And I want to read this line from a file and then assign it to fields of the variable sobre, type sbr
:
CoronelMostaza Candelabro Vestibulo
Essentially this is what I'm trying:
sobre.prj := CoronelMostaza;
sobre.arma := Candelabro;
sobre.habt := Vestibulo;
And this is what I've tried so far:
ReadLn(game, sobre.prj, sobre,arma, sobre.habt);
I'd tried turning off the IO check with no result, the closest I've got to the answer of this problem is assign the first word to the first variable, but always end in either an error or empty variables. Please help me and if you will, give me an idea of what's happening behind all this!
Assuming the text file contains the type literals of type pha
.
Declare aa text constant containing those literals:
const
phaStr : array[pha] of string = ('SenoraBlanco', 'SenorVerde', 'SenoraCeleste', 'ProfesorCiruela',
'SenoritaEscarlata', 'CoronelMostaza', 'Biblioteca', 'Cocina', 'Comedor', 'Estudio',
'Vestibulo', 'Salon', 'Invernadero', 'SalaDeBaile', 'SalaDeBillar', 'Candelabro',
'Cuchillo', 'Cuerda', 'LlaveInglesa', 'Revolver', 'Tubo');
To get an integer index of one of those string literals:
function IndexOfPhaStr( const aStr : string) : integer;
var
aPHA : pha;
begin
Result := -1;
for aPHA := Low(pha) to High(pha) do
begin
if (aStr = phaStr[aPHA]) then
begin
Result := Ord(aPHA);
break;
end;
end;
end;
The file contains text information, so you will have to translate the text into your data types. A simple example using TStringList
in classesh.inc
:
var
i : Integer;
sList : TStringList;
s : String;
ix : array [1..3] of integer;
begin
...
sList := TStringList.Create;
sList.Delimiter := ' ';
AssignFile(game,'TEST.Txt');
Reset(game);
System.ReadLn(game, s);
System.CloseFile(game);
sList.DelimitedText := s;
for i := 0 to 2 do
ix[i+1] := IndexOfPhaStr(sList[i]);
if (ix[1] in [Ord(Low(p))..Ord(High(p))])
then sobre.prj := pha(ix[1]);
if (ix[2] in [Ord(Low(a))..Ord(High(a))])
then sobre.arma := pha(ix[2]);
if (ix[3] in [Ord(Low(h))..Ord(High(h))])
then sobre.habt:= pha(ix[3]);
sList.Free;
end;
If your text file contains more information, do a text conversion for them as well.
PS. I'll leave the error handling as an exercise.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.