Delphi-Kylix
  Home arrow Delphi-Kylix arrow Page 2 - 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  
Moblin 
JMSL Numerical Library 
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


    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.

    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

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






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
    Stay green...Green IT