SunQuest
 
       Delphi-Kylix
  Home arrow Delphi-Kylix arrow Handling Files in Delphi
IBM developerWorks
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Actuate Whitepapers 
Moblin 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
DELPHI-KYLIX

Handling Files in Delphi
By: Jacques Noah
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 3
    2006-08-30

    Table of Contents:
  • Handling Files in Delphi
  • Reading from a file
  • Binary Files
  • Streams

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Handling Files in Delphi


    (Page 1 of 4 )

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

    More Delphi-Kylix Articles
    More By Jacques Noah


       · I hope this article is of use to those of you who is learning to program using...
     

    DELPHI-KYLIX ARTICLES

    - Client Dataset: Working with Data Packets an...
    - Using the Client Dataset in an N-Tiered Appl...
    - Using the Client Dataset in Two-Tiered Clien...
    - Using the Client Dataset in File-Based Archi...
    - Demystifying the Client Dataset
    - Working with INI Files in Delphi
    - Creating Data Link (UDL) Files in Delphi
    - Looking at the Details for an Invoicing Appl...
    - Invoicing in Delphi: Show Me the Money
    - Saving Images and Binary Files to a Database...
    - Saving Files to a Database using Delphi: Sav...
    - Creating CF Applications and Integrating a S...
    - Using Try and Finally to Help Prevent Memory...
    - The Implementation of an FTP Server
    - FTP Server: The Theory







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway