Important alert: (current site time 7/16/2013 5:09:21 AM EDT)
 

article

Quickly retrieve the lines from a text file

Email
Submitted on: 2/18/2001 11:10:53 PM
By: Bob Brown  
Level: Intermediate
User Rating: By 7 Users
Compatibility: Delphi 5, Delphi 4, Pre Delphi 4
Views: 17522
(About the author)
 
     This function quickly retrieves a range of lines from a text file.

 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
  1. You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
				
Quickly retrieve the lines from a text file

If you need a quick function to get a line from a text file, try this one. It's very simple to use, for example:

Writeln('The 2nd line in c:\autoexec.bat is '+GetLinesFromFile('c:\autoexec.bat',2,2)[0]);

functionGetLinesFromFile(filename:string; linestart, lineend:integer) : TStringList;
(*
// Name : GetLinesFromFile
// Purpose : Returns a series of lines from a text file, starting at linestart and going to lineend
// Date : 12 Feb 2001
// Comments :
*)
var
  fn : textfile;
  c : word;
  Line : String;
begin
  Result:=TStringList.Create;
  AssignFile(fn,filename);
  Reset(fn);
  for c:=0 to LineStart do
    ReadLn(fn,Line);
  for c:=LineStart to LineEnd do
  begin
    Result.Add(Line);
    ReadLn(fn,Line);
  end;
  CloseFile(fn);
end;

Please vote for this code!


Other 1 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

2/22/2001 9:09:08 AMjparks

Are you sure the TStringList is being destroyed? Just curious :-)
(If this comment was disrespectful, please report it.)

 
4/12/2001 4:53:46 PMfoahchon

You can just use TStringList.LoadFromFile or TStringList.SaveToFile in Delphi 5.
(If this comment was disrespectful, please report it.)

 
5/24/2001 7:40:15 PMBrian Hill

When I try to use the code I get this error message :
[Fatal Error] Unit7.pas(83): Internal error: C862
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.