Delphi-Kylix
  Home arrow Delphi-Kylix arrow Page 3 - Handling Files in Delphi
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  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
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


    Handling Files in Delphi - Binary Files


    (Page 3 of 4 )

    Delphi also provides us with tools to read and write typed and untyped binary files. 

    Typed Files

    Typed binary files are files that have a data type as the basic unit of writing and reading. You write a record to a file, and read the same unit of data back. Records are particularly useful, allowing us to store any mix of data types in the one file unit of data. Some people use records as a database to store their data. Although not very secure, it is very suitable for that purpose.

    The syntax of a record is like so:

    type

       Trecordname = Record

    fieldList1: type1;

     ...

    fieldListn: typen;

      end;

     

    For example:

    type
       Tpeople = Record
         name : string[20];
         age  : Integer;
         work:string;
       end;

    In the example above we have created a record of type Tpeople, with its members as name, age and work.

    You will notice that name is declared with string[20]. The -  '[20]' -  allocates twenty characters to this member. In other words, any name that you give has to be limited to twenty characters. If you don't give a number the string will be allocated space for 255 characters.

    To illustrate, we will create a record called people and  then demonstrate how to read and write to it.

    Create a new application and add the following components: three Tedits, one Memo, and two Buttons, one captioned "Read from Record" and the other captioned "Write to Record." Then add the following type above the form type definition:

    type
       Tpeople = Record
         name : string[20];
         age  : Integer;
         work:string;
       end;

    It is a very simple record that takes three values, the name of the person, the age and the work they do. To actually use the record, we need to create a record variable like so:

    variablename: recordtype;

    so in the form variable section add the following code:

    person:Tpeople;

    We are going to use the person variable to access the record data.

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

     procedure TForm1.Button1Click(Sender: TObject);
    var
     f:file of Tpeople;
    begin
    AssignFile(F, 'person.per');
    ReWrite(F);
        person.name:=edit1.Text;
        person.age:=strtoint(edit2.Text);
        person.work:=edit3.Text;
     Write(f,person);
      // Close the file
       CloseFile(f);
    end;

    This procedure is writing the record data to a file called  "person.per". Before we can write record data to the disk we need to declare a file type variable. This variable is declared like so:

    Var variablename: file of Record;

    In our code we declared it as f: file of Tpeople;

    The next step is to assign the file variable to a file on a disk. The code:

    AssignFile(F, 'person.per');

    does the job for us. Once all the file connections have been set, the record is filled with data from the Tedits:

        person.name:=edit1.Text;

        person.age:=strtoint(edit2.Text);

        person.work:=edit3.Text;

    To access the fields of the record we use the record variable -person - with the dot notation. With the new record values safely stored in the person variable, the record data is written to the file like so:

    Write(f,person);

    And then file is closed with the closefile() function.

    Double click on the "Read from Record" button and add the following code:

    procedure TForm1.Button2Click(Sender: TObject);
    var
    F  : File of tpeople;  // A file of personrecords
     begin
      AssignFile(F, 'person.per');
       Reset(f);
       // Display the file contents
       while not Eof(f) do
       begin
         Read(f, person);
     memo1.lines.Add(person.name+#13#10+inttostr(person.age)
    +#13#10+person.work);
       end;
       // Close the file for the last time
       CloseFile(f);
    end;

    After assigning the file variable, the code runs a while loop and reads all of the record entries from the file:

       while not Eof(f) do

       begin

         Read(f, person);

     emo1.lines.Add(person.name+#13#10+inttostr(person.age)+#13#10+person.work);

       end;

    The Eof (f) stands for end of file, and tells the while loop to read until it reaches the end of the file. All the contents are then added to the memo.

    As you can see, using typed binary files does have advantages. You can store as many records as you like and even go to specific entries in a record. This almost gives it a database-like functionality. Delphi provides us with a couple of functions to move around to different positions within a file:

    Seek() function enables us to go to specific locations in a record. The syntax is:

    Seek(file variable, position);
    For example, to go to the fourth place in a record:
    Seek(F, 5);
    Or to go to the end after the last record:
    Seek(F, FileSize(F));

    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

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







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek