Recent

Author Topic: Attempting to copy a textfile backwards  (Read 2670 times)

eerieghoul

  • Newbie
  • Posts: 1
Attempting to copy a textfile backwards
« on: May 12, 2021, 09:24:22 am »
Hi, could anyone please tell me what I'm doing wrong? I'm supposed to read from the input.txt, which contains several lines of text, for example:

pen
paper
eraser

...and it's supposed to invert the order of the lines, such as:

eraser
paper
pen

But, the output file is always blank and I can't seem to figure out why.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   input, output: textfile;
  4.   line: string;
  5.   i: word;
  6. begin
  7.   assignfile(input, 'input.txt');
  8.   assignfile(output, 'output.txt');
  9.  
  10.   reset(input);
  11.   rewrite(output);
  12.  
  13.   i:= 0;
  14.  
  15.   while not eof(input) do // finds the amount of lines
  16.   begin
  17.     readln(input);
  18.     inc(i);
  19.   end;
  20.  
  21.   while i > 0 do
  22.   begin
  23.     readln(input, line);
  24.     writeln(output, line);
  25.     dec(i); // moves up by one line each iteration
  26.   end;
  27.  
  28.   closefile(input);
  29.   closefile(output);
  30. end;
  31. end.  
  32.  

Any help would be appreciated.  :)

mdalacu

  • Full Member
  • ***
  • Posts: 233
    • dmSimpleApps
Re: Attempting to copy a textfile backwards
« Reply #1 on: May 12, 2021, 09:31:39 am »
Because you are always reading the last line from input file in the second while.

egsuh

  • Hero Member
  • *****
  • Posts: 1266
Re: Attempting to copy a textfile backwards
« Reply #2 on: May 12, 2021, 09:32:30 am »
You cannot read (or write) text file back. You can only read from the start to the end.
If you want to write the sentences in the reverse order they read, you have to define array, etc.

Code: Pascal  [Select][+][-]
  1.  procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   input, output: textfile;
  4.   line: string;
  5.   i: word;
  6.   lines: array of string;
  7. begin
  8.   assignfile(input, 'input.txt');
  9.   assignfile(output, 'output.txt');
  10.  
  11.   reset(input);
  12.  
  13.   // i:= 0;
  14.  
  15.   while not eof(input) do // finds the amount of lines
  16.   begin
  17.       readln(input, line);
  18.       SetLength(Lines, Length(Lines) + 1);
  19.       Lines[High(Lines)] := line;
  20.   end;
  21.   closefile(input);
  22.    
  23.    rewrite(output);
  24.    for i:= High(Lines) downto Low(Lines) do
  25.        writeln(output, Lines[i]);
  26.  
  27.   closefile(output);
  28. end;

mdalacu

  • Full Member
  • ***
  • Posts: 233
    • dmSimpleApps
Re: Attempting to copy a textfile backwards
« Reply #3 on: May 12, 2021, 09:37:52 am »
You should real the whole file in a TStringList with LoadFromFile method. and read strings in reverse order, writing them to your output file. Simple.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Attempting to copy a textfile backwards
« Reply #4 on: May 12, 2021, 09:50:50 am »
"Input" and "output" have predefined meaning in Pascal.
You cannot use these as identifiers and expect them to mean something different.
If you don't want to track the lines using a class such as TStringlist you can use a simple dynamic array:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   inp, outp: TextFile;
  4.   lines: TStringArray = Nil;
  5.   i: Integer;
  6. begin
  7.   AssignFile(inp, 'input.txt');
  8.   try
  9.     Reset(inp);
  10.     while not EOF(inp) do
  11.     begin
  12.       SetLength(lines, Succ(Length(lines)));
  13.       ReadLn(inp, lines[High(lines)]);
  14.     end;
  15.    
  16.     AssignFile(outp, 'output.txt');
  17.     try
  18.       Rewrite(outp);
  19.       for i := High(lines) downto 0 do
  20.         WriteLn(outp, lines[i]);
  21.     finally
  22.       CloseFile(outp);
  23.     end;
  24.   finally
  25.     CloseFile(inp);
  26.   end;
  27. end;

MarkMLl

  • Hero Member
  • *****
  • Posts: 6646
Re: Attempting to copy a textfile backwards
« Reply #5 on: May 12, 2021, 10:02:23 am »
Hi, could anyone please tell me what I'm doing wrong? I'm supposed to read from the input.txt, which contains several lines of text, for example:

Considering what people have said already: is this something that you're having to do as part of a student assignment ("homework" :-) using classic Pascal, or is this something that you're free to do any way you want?

The point's already been made that you can't redefine input and output, classical Pascal texts will often show that the first line of a program reads

Code: Pascal  [Select][+][-]
  1. program(input, output);
  2.  

and in the general case those identifiers should be treated as reserved.

You are unlikely to be able to seek inside a text file, unless you start handling all the I/O yourself. At best, I'd expect the availability of that to be implementation-defined.

That means that you're probably going to need to put the lines you read into an array or list. However the detail of that depends on what facilities you're prepared or allowed to use.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Thaddy

  • Hero Member
  • *****
  • Posts: 14157
  • Probably until I exterminate Putin.
Re: Attempting to copy a textfile backwards
« Reply #6 on: May 12, 2021, 01:22:41 pm »
If it is homework, you require a stack. One of the basic structures.
Specialize a type, not a var.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5444
  • Compiler Developer
Re: Attempting to copy a textfile backwards
« Reply #7 on: May 12, 2021, 01:27:14 pm »
"Input" and "output" have predefined meaning in Pascal.
You cannot use these as identifiers and expect them to mean something different.

Input and Output are merely identifiers provided in the System unit or - if provided - the program header (in which case they'll only mean something in case of the ISO modes). Other than that they're just as reusable as e.g. Writeln.

avk

  • Hero Member
  • *****
  • Posts: 752
Re: Attempting to copy a textfile backwards
« Reply #8 on: May 12, 2021, 02:22:05 pm »
If it is homework, you require a stack. One of the basic structures.

One can also imagine a recursive procedure that does this without an explicit stack.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6646
Re: Attempting to copy a textfile backwards
« Reply #9 on: May 12, 2021, 02:30:22 pm »
"Input" and "output" have predefined meaning in Pascal.
You cannot use these as identifiers and expect them to mean something different.

Input and Output are merely identifiers provided in the System unit or - if provided - the program header (in which case they'll only mean something in case of the ISO modes). Other than that they're just as reusable as e.g. Writeln.

I obviously can't speak for Howard, but when I echoed the warning I was trying to be very cautious about any assumptions relating to language implementation etc... if it's homework we don't know whether the school/college uses FPC, and while I'm definitely not criticising OP for asking here since he's presumably grabbed himself a copy of FPC and is clearly trying harder than some people with the same sort of request I think that we need to keep our advice as general as possible.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

MarkMLl

  • Hero Member
  • *****
  • Posts: 6646
Re: Attempting to copy a textfile backwards
« Reply #10 on: May 12, 2021, 02:34:17 pm »
One can also imagine a recursive procedure that does this without an explicit stack.

That of course is an interesting point. If OP is on or preparing for a Computer Science course then demonstrating knowledge of recursion might get him an extra mark, while if the course is Engineering and he hasn't been told how much data to expect relative to the specification of the computer he's working on it might have the opposite effect :-)

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

cdbc

  • Hero Member
  • *****
  • Posts: 995
    • http://www.cdbc.dk
Re: Attempting to copy a textfile backwards
« Reply #11 on: May 12, 2021, 02:39:17 pm »
Hi
You could have a look at these datastructures by Julian Bucknall: https://github.com/cdbc-dk/bc_rtl/tree/master/ezdsl
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

avk

  • Hero Member
  • *****
  • Posts: 752
Re: Attempting to copy a textfile backwards
« Reply #12 on: May 12, 2021, 02:56:54 pm »
One can also imagine a recursive procedure that does this without an explicit stack.

That of course is an interesting point. If OP is on or preparing for a Computer Science course then demonstrating knowledge of recursion might get him an extra mark, while if the course is Engineering and he hasn't been told how much data to expect relative to the specification of the computer he's working on it might have the opposite effect :-)

MarkMLl

I don't remember exactly anymore, but I suppose recursion occurred before data structures in my school days. :-\

jamie

  • Hero Member
  • *****
  • Posts: 6077
Re: Attempting to copy a textfile backwards
« Reply #13 on: May 13, 2021, 12:30:31 am »
This reminds me of my old school mainframe and under powered computer use.

Step one
 Open the input file
 
Step two
   Read a line of text from the input file

Step three
  Write this line to a new temp file

Step four
  Append a trailing file to this temp file

Step Five
  Delete the trailing file and rename the temp to the trailing file name.

Step 6ix
   repeat Step two until no more input lines.

Step Seven
   Close all files

Now your input file is backwards in the trailing file, what ever file name you give it.
The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018