Solved

Super easy question...

Posted on 1998-12-05
8
138 Views
Last Modified: 2010-04-06
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
Comment
Question by:dokken
  • 4
  • 3
8 Comments
 
LVL 17

Expert Comment

by:inthe
ID: 1349456
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
 
LVL 17

Expert Comment

by:inthe
ID: 1349457
quick syntax check...


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

Barry

0
 

Author Comment

by:dokken
ID: 1349458
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
ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 

Author Comment

by:dokken
ID: 1349459
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
 
LVL 17

Expert Comment

by:inthe
ID: 1349460
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
 

Author Comment

by:dokken
ID: 1349461
Thats works great, put it as the answer so I can give you the points.

Thanks.
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1349462
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
 
LVL 17

Accepted Solution

by:
inthe earned 20 total points
ID: 1349463
as awnser:
var
mystring:tstringlist;
begin
mystring:=tstringlist.create;
mystring.loadfromfile('c:\autoexec.bat');
{do whatever with your string}
end

Barry
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question