Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2004-08

dumb question

3 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Unknown author

Ok, guys. it's only been like 15 years since I have written any pascal code. Can someone please help me with syntax for opening a file so that I can read variable length lines of text from it, then output to another file. Running in DOS on an x86 machine. Thank you.

Jason

Reply by email

Michael Van Canneyt
to Unknown author
Ok, guys. it's only been like 15 years since I have written any pascal code. Can someone please help me with syntax for opening a file so that I can read variable length lines of text from it, then output to another file. Running in DOS on an x86 machine. Thank you.

Here you are. Standard textbook examples. Also available in the manual.

Michael.

procedure ReadFile(FN : String);
Var
  F : Text;
  S : String;
begin
  Assign(F,FN);
  Reset(F);
  While not EOF(F) do
    begin
    ReadLn(F,S);
    { Do something with S. }
    end;
  Close(F);
end;
procedure WriteFile(FN : String);
Var
  F : Text;
  I : Integer;
  S : String;
begin
  Assign(F,FN);
  Rewrite(F);
  For I:=0 to 100 do
    Writeln(F,'Writing string ',I);
  Close(F);
end;

Reply by email

Jilani Khaldi
to Unknown author: Ok, guys. it's only been like 15 years since I have written any pascal code. Can someone…
Show quoted text 5 lines
Ok, guys. it's only been like 15 years since I have written any pascal code. Can someone please help me with syntax for opening a file so that I can read variable length lines of text from it, then output to another file. Running in DOS on an x86 machine. Thank you.

Hi Jason,

procedure ReadFile(const FileName: string);
var
  f: TextFile;
  st: string;
begin
  assignfile(f,FileName);
  reset(f);
  while not eof(f) do
  begin
    readln(f, st); // here you are reading one line from the file at
every loop
    // do what you want with st
    writeln(st); // for example
  end;
  closefile(f);
end;
// main
begin
  ReadFile('c:\autoexec.bat');
end.
Jilani

Reply by email