Reverse the order of lines in a text file while preserving the contents of each line: Difference between revisions
Reverse the order of lines in a text file while preserving the contents of each line (view source)
Revision as of 19:00, 23 May 2022
, 1 year ago→Pascal: move contents to →Free Pascal: and provide ISO-compliant source code, create →Delphi: referencing →Free Pascal
Alextretyak (talk | contribs) (Added 11l) |
(→Pascal: move contents to →Free Pascal: and provide ISO-compliant source code, create →Delphi: referencing →Free Pascal) |
||
Line 2:
;Task:
:* Read
:* 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/>
The following is an ISO-compliant <tt>program</tt>.
Some compilers, however, such as the FPC (Free Pascal Compiler) or Delphi, cannot handle files without file names.
<lang pascal>program tac(input, output);
procedure reverse;
var
line: text;
begin
{ Open for (over-)writing. }
rewrite(line);
{ `EOLn` is shorthand for `EOLn(input)`. }
while not
begin
{ `line^` and `input^` refer to buffer variables [their values]. }
line^ := input^;
{ Write buffer and advance writing position. }
put(line);
{ Advance reading cursor and obtain next value [if such exists]. }
get(input)
end;
{ 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
reverse
end.</lang>
{{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}}==
|