?
Solved

Super easy question...

Posted on 1998-12-05
8
Medium Priority
?
142 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 80 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

770 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