Forum > Beginners

Attempting to copy a textfile backwards

(1/3) > >>

eerieghoul:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.Button1Click(Sender: TObject);var  input, output: textfile;  line: string;  i: word;begin  assignfile(input, 'input.txt');  assignfile(output, 'output.txt');   reset(input);  rewrite(output);   i:= 0;   while not eof(input) do // finds the amount of lines  begin    readln(input);    inc(i);  end;   while i > 0 do  begin    readln(input, line);    writeln(output, line);    dec(i); // moves up by one line each iteration  end;   closefile(input);  closefile(output);end;end.   
Any help would be appreciated.  :)

mdalacu:
Because you are always reading the last line from input file in the second while.

egsuh:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- procedure TForm1.Button1Click(Sender: TObject);var  input, output: textfile;  line: string;  i: word;  lines: array of string; begin  assignfile(input, 'input.txt');  assignfile(output, 'output.txt');   reset(input);   // i:= 0;   while not eof(input) do // finds the amount of lines  begin      readln(input, line);      SetLength(Lines, Length(Lines) + 1);      Lines[High(Lines)] := line;   end;  closefile(input);      rewrite(output);   for i:= High(Lines) downto Low(Lines) do        writeln(output, Lines[i]);    closefile(output);end;

mdalacu:
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:
"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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.Button1Click(Sender: TObject);var  inp, outp: TextFile;  lines: TStringArray = Nil;  i: Integer;begin  AssignFile(inp, 'input.txt');  try    Reset(inp);    while not EOF(inp) do    begin      SetLength(lines, Succ(Length(lines)));      ReadLn(inp, lines[High(lines)]);    end;        AssignFile(outp, 'output.txt');    try      Rewrite(outp);      for i := High(lines) downto 0 do        WriteLn(outp, lines[i]);    finally      CloseFile(outp);    end;  finally    CloseFile(inp);  end;end;

Navigation

[0] Message Index

[#] Next page

Go to full version