[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 144
  • Last Modified:

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.
0
dokken
Asked:
dokken
  • 4
  • 3
1 Solution
 
intheCommented:
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

0
 
intheCommented:
quick syntax check...


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

Barry

0
 
dokkenAuthor Commented:
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.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
dokkenAuthor Commented:
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.
0
 
intheCommented:
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?
0
 
dokkenAuthor Commented:
Thats works great, put it as the answer so I can give you the points.

Thanks.
0
 
MadshiCommented:
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.
0
 
intheCommented:
as awnser:
var
mystring:tstringlist;
begin
mystring:=tstringlist.create;
mystring.loadfromfile('c:\autoexec.bat');
{do whatever with your string}
end

Barry
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now