SRL Archive - OSR Help

Read and store each line of a file to a variable


tom99

2010-05-22 18:33:20

Read and store each line of a file to a variable from a .txt file how can this be done?
i need it for a database utility.

Sex

2010-05-22 18:53:38

program New;

const
FilePath = 'C:\test.txt';
var
FileNum, i, h : integer;
s : string;
Arr : TStringArray;
begin
if not FileExists(FilePath) then
begin
Writeln('File does not exist.');
TerminateScript;
end;
FileNum := OpenFile(Filepath, False);
ReadFileString(FileNum, s, FileSize(FileNum));
CloseFile(FileNum);
Arr := Explode(#13#10, s);
h := high(Arr);
for i := 0 to h do
Writeln(Format('Line %d: %s', [i + 1, Arr[i]]));
end.

tom99

2010-05-22 20:26:19

This is not what i wanted , i need a script witch will save the information in each line into a variable, forexample line 1 woud be sheep1 next sheep2 and so on.

tom99

2010-05-22 20:48:24

Is there a way to do this? any help please ?

A G E N T

2010-05-22 20:54:54

The example Sex posted works fine, and unless there is a pressing reason that you need to have the strings stored as "sheep1", "sheep2" rather than arr[0], arr[1], etc, you can use it as is.
If you really need to have them stored as individual variables I can think of something you could do, but it is going to be kludgy and inflexible…

tom99

2010-05-22 20:59:41

Yes they need to be set as idividual variables could it be done with TIntegerArray variable you think ?
i want it to be like i can do writeln(sheep7); whenever i want in the script so the string on line 7 was saved to that variable.

Dan Cardin

2010-05-22 21:03:14

yea, i dont see whats wrong with what he did. Just think of each value in arr as a separate value. arr[0], arr[1]

tom99

2010-05-22 21:05:48

Ah like that now i understand thank you very much.