1
0
lazarus-tutorials/text_file/text_file.lpr

48 lines
868 B
ObjectPascal
Raw Permalink Normal View History

program text_file;
{$mode objfpc}{$H+}
uses sysutils;
var
2021-09-30 14:35:36 -04:00
txtF: TextFile;
s: String;
procedure ReadTenLines;
const lineNo: integer = 0;
var linesRead: integer = 0;
begin
2021-09-30 14:35:36 -04:00
while not EOF(txtF) and (linesRead < 10) do
begin
ReadLn(txtF, s); // Read the current line in the file, and store in s
Inc(linesRead); // linesRead += 1
Inc(lineNo);
s := Format('Line %d: %s', [lineNo, s]);
WriteLn(s);
end;
end;
begin
2021-09-30 14:35:36 -04:00
AssignFile(txtF, 'text_file.lpr'); // Open pointer to the file
Reset(txtF); // Open existing file
WriteLn('Lines from text_file.lpr will be displayed 10 at a time');
WriteLn;
// Read and display the file
try
while not EOF(txtF) do
begin
ReadTenLines;
WriteLn;
WriteLn('Press [Enter] to continue');
ReadLn;
end;
finally
CloseFile(txtF);
end;
Write('End of file reached. Press [Enter] to finish');
ReadLn;
end.