How can I get contents from a local File?

If you need help on how to use the program
Post Reply
fulvio53s03
Posts: 764
Joined: 2007-04-28 05:46:43
Location: Italy

How can I get contents from a local File?

Post by fulvio53s03 »

I have a local file "c:\commenti.txt" and I would like to extract lines containing certain words:
now
I like
this
program.
This
is the reason
why
I want to extract
this
.
How can I open the file and filter it?
Thank you! ;)
antp
Site Admin
Posts: 9651
Joined: 2002-05-30 10:13:07
Location: Brussels
Contact:

Re: How can I get contents from a local File?

Post by antp »

The TStringList class has LoadFromFile and SaveToFile functions that you can use for that:

Code: Select all

function GetLocalFile(Path: string): string;
var
  File: TStringList;
  FileName: string;
begin
  File:= TStringList.Create;
  FileName := Path;
  Input('', 'File to read:', FileName);
  File.LoadFromFile(FileName);
  Result := File.Text;
  File.Free;
end;
This is a demo function. If you want to process it line per line, you can of course directly use the TStringList object (named "File" here)
fulvio53s03
Posts: 764
Joined: 2007-04-28 05:46:43
Location: Italy

Re: How can I get contents from a local File?

Post by fulvio53s03 »

:grinking: i'll try!
Post Reply