Home arrow Delphi-Kylix arrow Page 2 - Handling Files in Delphi
DELPHI-KYLIX

Handling Files in Delphi


Handling files in Delphi is not so different from handling files in any other high level programming language. In this article we will examine how to read and write to text files and binary files and we will also explore various methods of accessing files.

Author Info:
By: Jacques Noah
Rating: 5 stars5 stars5 stars5 stars5 stars / 5
August 30, 2006
TABLE OF CONTENTS:
  1. · Handling Files in Delphi
  2. · Reading from a file
  3. · Binary Files
  4. · Streams

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Handling Files in Delphi - Reading from a file
(Page 2 of 4 )

There are two ways to read from a file:

  • Line by line
  • Reading the entire contents into a string list.

Line by line

To read the file line by line we need to open the file for input by calling the reset procedure:

var
  Afile : TextFile;
  buffer : string;
begin
  AssignFile(Afile, 'notes.txt') ;

try
  Reset(Afile);
  ReadLn(Afile, buffer) ;
  Memo2.Lines.Add(buffer) ;

finally
  CloseFile(Afile) ;
end; 

end;

There are two types of read procedures, readln and read. The difference is that Readln moves to the pointer to the next line after reading and Read does not. Here's how you would read from a file using Read procedure:

var
  Afile : TextFile;
  buffer : string
begin
  AssignFile(Afile, 'notes.txt') ;

try
  Reset(Afile);
  Read(Afile, buffer) ;
  Memo2.Lines.Add(buffer) ;

finally
  CloseFile(Afile) ;
end; 

end;

Reading the entire contents of a file

Double click on the "read from file" button and add the following code:

procedure TForm1.Button2Click(Sender: TObject);
begin
Memo2.Lines.LoadFromFile('notes.txt');
end;

We use tmemo's loadfromfile() procedure to load the entire contents of a file.


blog comments powered by Disqus
DELPHI-KYLIX ARTICLES

- Loading an XML Document into the DOM
- Delphi Wrapper Classes and XML
- Delphi and the DOM
- Delphi and XML
- Internet Access: Client Service
- Finishing the Client for an Internet Access ...
- The Client for an Internet Access Control Ap...
- User Management for an Internet Access Contr...
- Important Procedures for an Internet Access ...
- Server Code for an Internet Access Control A...
- Constructing the Interface for an Internet A...
- Building a Server Application for an Interne...
- Building an Internet Access Control Applicat...
- Client Dataset: Working with Data Packets an...
- Using the Client Dataset in an N-Tiered Appl...

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 5 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials