Home arrow Delphi-Kylix arrow 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 / 8
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

Handling Files in Delphi
(Page 1 of 4 )


Downloadable files for this article are available here and here.

Required

Borland Delphi, any version.

Text Files

Example file handling program..

In short, text files contain readable ASCII characters.  The text within a text file is characterized by lines that are terminated by end-of-line characters.

To open a text file, we need to link a file that is on the local disk with a variable in our code. We do this by using the AssignFile() method. First declare a variable of type text file and then assign that variable with a file on your disk through the AssignFile() procedure.

Example:

var Afile:textfile;
begin
AssignFile(Afile,Afilename);
...
end;

Once you've associated the file with a variable, you are now free to read and write to this file by referring to it by its variable name: Afile. Once you have finished reading and writing to that file, you call the closefile(Afile) procedure to signal that you have finished using the file.

Enough of the theory, let's write to an example file:

In Delphi, create a new application and add two tmemo components. Then add two buttons with the captions Write to File and Read from File, respectively.

Double click on the "write to file" button and add the following code:

var
Afile:textfile;
i:integer;
begin
if memo1.lines.Text = '' then begin
showmessage('Please enter some text');
end
else begin
assignfile(Afile,'notes.txt');
try
 rewrite(Afile);//create this file if it does not exist,
otherwise open it.
for i:= 0 to (-1 + Memo1.Lines.Count) do
    WriteLn(Afile, Memo1.Lines[i]) ;
finally
closefile(Afile);
end;
end;
end;

Also, try to use the try/finally blocks for exception handling, as I/O can be full of surprises. The rewrite procedure creates a new file and moves the file pointer to the beginning of the file. If a file with the same name already exists, it is deleted and a new empty file is created in its place. So if you do not want to lose the data in a file, but want to add some data to it, you use the append() procedure.

Append procedure

The append procedure enables you to add data to an existing file. To demonstrate, add another button and a memo to our application. Double click on the button and add the following code:

procedure TForm1.Button3Click(Sender: TObject);
var
  Afile:textfile;
S:string;
begin
AssignFile(Afile,'notes.txt') ;
  try
  Append(Afile) ;
 s:=memo3.Lines.Text;
  Writeln(Afile,s);
      finally
  CloseFile(AFile) ;
end;
end;

The append procedure positions the file pointer at the end 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...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




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