Reverse the order of lines in a text file while preserving the contents of each line: Difference between revisions

→‎Pascal: move contents to →‎Free Pascal: and provide ISO-compliant source code, create →‎Delphi: referencing →‎Free Pascal
(Added 11l)
(→‎Pascal: move contents to →‎Free Pascal: and provide ISO-compliant source code, create →‎Delphi: referencing →‎Free Pascal)
Line 2:
 
;Task:
:*   Read an an entire (input) file   (into memory or buffers).
:*   Display the lines/records of the entire file in reverse order.
:*   Show the results here, on this page.
Line 234:
</pre>
 
=={{header|Delphi}}==
''See maybe [[#Free Pascal|Free Pascal]]''
 
=={{header|FreeBASIC}}==
Line 261 ⟶ 263:
</pre>
 
=={{header|Free Pascal}}==
<lang pascal>program TAC;
{$IFDEF FPC}
{$MODE DELPHI}
{$ELSE}
{$APPTYPE CONSOLE}
{$ENDIF}
uses
sysutils, classes;
var
Sl:TStringList;
i,j : nativeInt;
begin
Sl := TStringList.Create;
Sl.Loadfromfile('Rodgers.txt');
i := 0;
j := Sl.Count-1;
While i<j do
Begin
Sl.Exchange(i,j);
inc(i);
dec(j);
end;
writeln(Sl.text);
end.</lang>{{out}}
<pre> --- Will Rodgers
 
until you can find a rock."
saying 'Nice Doggy'
"Diplomacy is the art of</pre>
 
=={{header|Go}}==
Line 387 ⟶ 419:
 
=={{header|Pascal}}==
''See also [[#Delphi|Delphi]] and [[#Free Pascal|Free Pascal]]''<br/>
{{works with|Free Pascal}} maybe {{works with|Delphi}}
The following is an ISO-compliant <tt>program</tt>.
<lang pascal>program TAC;
Some compilers, however, such as the FPC (Free Pascal Compiler) or Delphi, cannot handle files without file names.
{$IFDEF FPC}
<lang pascal>program tac(input, output);
{$MODE DELPHI}
 
{$ELSE}
procedure reverse;
{$APPTYPE CONSOLE}
{$ENDIF}
uses
sysutils, classes;
var
line: text;
Sl:TStringList;
i,j : nativeInt;
begin
{ Open for (over-)writing. }
Sl := TStringList.Create;
rewrite(line);
Sl.Loadfromfile('Rodgers.txt');
i := 0;
{ `EOLn` is shorthand for `EOLn(input)`. }
j := Sl.Count-1;
while not While i<jEOLn do
begin
Begin
{ `line^` and `input^` refer to buffer variables [their values]. }
Sl.Exchange(i,j);
line^ := input^;
inc(i);
{ Write buffer and advance writing position. }
dec(j);
put(line);
end;
{ Advance reading cursor and obtain next value [if such exists]. }
writeln(Sl.text);
get(input)
end.</lang>{{out}}
end;
<pre> --- Will Rodgers
{ Consume “newline” character in `input` }
readLn;
{ Likewise, `EOF` is shorthand for `EOF(input)`. }
if not EOF then
begin
reverse
end;
{ (Re‑)open for reading. }
reset(line);
while not EOLn(line) do
begin
output^ := line^;
put(output);
get(line)
end;
{ `writeLn` is shorthand for `writeLn(output)`. }
writeLn
end;
 
begin
until you can find a rock."
reverse
saying 'Nice Doggy'
end.</lang>
"Diplomacy is the art of</pre>
{{out}}
--- Will Rodgers
until you can find a rock."
saying 'Nice Doggy'
"Diplomacy is the art of
Note, this <tt>program</tt> will append a “new line” character to the final line if it did not exist.
 
=={{header|Perl}}==
149

edits