Link to home
Start Free TrialLog in
Avatar of dokken
dokken

asked on

Super easy question...

I'm new to Delphi and would like to know how to read a text file into a string.  It has to be fast because the file it's reading could be over 5 MB's.

Thanks.
Avatar of inthe
inthe

hi,
use loadfromfile with begin and end update ie:

application.processmessages;
memo1.lines.beginupdate;
memo1.lines.loadfromfile(c:\autoexec.bat);
memo1.lines.enupdate;

a good way to load a large file is to show a splash screen while its loading
Barry

quick syntax check...


memo1.lines.beginupdate;
application.processmessages;
memo1.lines.loadfromfile('c:\autoexec.bat');
memo1.lines.endupdate;

Barry

Avatar of dokken

ASKER

I already know how to load it into a memo and rich text box... but I only want it load into a string.  The file is going to get very big, so I don't want any limitations or anything slowing it down.
Avatar of dokken

ASKER

Or, what might be better, if the rich text box's savetofile can save the text as text and not RTF and handle over 200,000 lines.
hi
sorry ok what about

var
mystring:tstringlist;
begin
mystring:=tstringlist.create;
mystring.loadfromfile('c:\autoexec.bat');
{do whatever with your string}
end

this will read your file into 'mystring' im memory and should be quite fast enough, the size wont be a prob stringlists can handle up to 2gig.
is this more on the right lines for your question?
Avatar of dokken

ASKER

Thats works great, put it as the answer so I can give you the points.

Thanks.
Or is it this what you want? It's the fastest way I can think of...

function LoadStrFromFile(filename: string; var str: string) : boolean;
var s1 : TFileStream;
begin
  result:=false;
  if not fio_FileExists(filename) then exit;
  s1:=TFileStream.Create(filename,fmOpenRead or fmShareDenyNone);
  try
    SetLength(str,s1.Size);
    s1.Read(PChar(str)^,s1.Size);
  finally s1.Free end;
  result:=true;
end;

Regards, Madshi.
ASKER CERTIFIED SOLUTION
Avatar of inthe
inthe

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial