Page 1 of 1

How do I read from a file?

Posted: 26 Jan 2015 08:52
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.

Re: How do I read from a file?

Posted: 26 Jan 2015 11:56
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.

Re: How do I read from a file?

Posted: 26 Jan 2015 12:08
by pjj
Thanks a lot for your prompt reply! The JS method works alright :D