How do I read from a file?

Ask questions about how to create a script or swap scripts with other users.
Post Reply
User avatar
pjj
Posts: 1784
Joined: 13 Oct 2009 13:48
Location: Kraków, Poland

How do I read from a file?

Post by pjj »

I want to read from (ANSI) file; how do I do it?

I tried

Code: Select all

var text = WStrings.LoadFromFile("filename.txt");
but then

Code: Select all

ShowMessage(text);
displays a number (always the same: 151894672) and not file content. Perhaps it's a bug?

Then I tried opening the file with MainApp.OpenFile and then using GetText, which worked, but 1) I'd prefer to read it in a "silent" mode, and 2) I'm not sure how to close a non-current document...

Then I tried TFileStream, but samples shown in the help file raised some errors -- first line

Code: Select all

TFileStream stream = TFileStream.Create("C:\\Test.txt");
lacks Mode, but even after adding it RJ TE complained ("Not enough actual parameters"), which finally discouraged me from streams.
Alium tibi quaere fratrem; hic, quem tuum putas, meus est. Titus Flāvius Caesar Vespasiānus Augustus

User avatar
Rickard Johansson
Site Admin
Posts: 6001
Joined: 19 Jul 2006 14:29

Re: How do I read from a file?

Post by Rickard Johansson »

You could try:

Code: Select all

TStringList list = TStringList.Create;
try
  list.LoadFromFile("C:\\Test.txt");
  var text = list.Text;
  ...
finally
   list.Free;
end;
or

Code: Select all

WStrings.LoadFromFile("filename.txt");
var text = WStrings.GetText;
I'll look into the stream issue.

User avatar
pjj
Posts: 1784
Joined: 13 Oct 2009 13:48
Location: Kraków, Poland

Re: How do I read from a file?

Post by pjj »

Thanks a lot for your prompt reply! The JS method works alright :D
Alium tibi quaere fratrem; hic, quem tuum putas, meus est. Titus Flāvius Caesar Vespasiānus Augustus

Post Reply