1. Technology

Managing Ascii (Text) Files from Code

By

Hands of a Businessman Typing on Laptop
TommL/E+/Getty Images

Simply put, text files contain readable ASCII characters. We can think of working with text file in Delphi as analogous to playing or recording information on a VCR tape.
Although it is possible to make changes within text file, jump around when processing information or add some data to the file other than at the end, it is advisable to use a text file only when we know that we are working with ordinary text and no such operations are necessary.

Text files are considered to represent a sequence of characters formatted into lines, where each line is terminated by an end-of-line marker (a CR/LF combination).

The TextFile and the Assign Method

To start working with text files you have to link a file on a disk to a file variable in your code - declare a variable of type TextFile and use the AssignFile procedure to associate a file on a disk with a file variable.
 var
   SomeTxtFile : TextFile;
 begin
   AssignFile(SomeTxtFile, FileName) 

Reading information from a Text File

If we want to read back the contest of a file into a string list, just one line of code will do the job.
 Memo1.Lines.LoadFromFile('c:\autoexec.bat') 
To read information from a file line by line, we must open the file for input by using the Reset procedure. Once a file is reset, we can use ReadLn to read information from a file (reads one line of text from a file then moves to the next line) :
 var
   SomeTxtFile : TextFile;
   buffer : string;
 begin
   AssignFile(SomeTxtFile, 'c:\autoexec.bat') ;
   Reset(SomeTxtFile) ;
   ReadLn(SomeTxtFile, buffer) ;
   Memo1.Lines.Add(buffer) ;
   CloseFile(SomeTxtFile) ;
 end; 

After adding one line of text from a file to a memo component SomeTxtFile needs to be closed. This is done by the Close keyword.

We can also use Read procedure to read information from a file. Read works just like ReadLn, except it does not move the pointer to the next line.

 var
   SomeTxtFile : TextFile;
   buf1,buf2 : string[5];
 begin
   AssignFile(SomeTxtFile, 'c:\autoexec.bat') ;
   Reset(SomeTxtFile) ;
   ReadLn(SomeTxtFile, buf1,buf2) ;
   ShowMessage(buf1 + ' ' +buf2) ;
   CloseFile(SomeTxtFile) ;
 end; 

EOF - End Of File

Use the EOF function to make sure that you are not trying to read beyond the end of the file. Let's say we want to display the contest of the file in message boxes - one line at a time, until we get to the end of a file:
 var
   SomeTxtFile : TextFile;
   buffer : string;
 begin
   AssignFile(SomeTxtFile, 'c:\autoexec.bat') ;
   Reset(SomeTxtFile) ;
   while not EOF(SomeTxtFile) do
   begin
    ReadLn(SomeTxtFile, buffer) ;
    ShowMessage(buffer) ;
   end;
   CloseFile(SomeTxtFile) ;
 end; 
Note: It is better to use While loop than the Until loop to take into account the (unlikely) possibility that the file exists but does not contain any data.

Writing Text to a File

The WriteLn is probably the most common way to send individual pieces of information to a file. The following code will read a text from a Memo1 component (line by line) and send it to some newly created text file.
 var
   SomeTxtFile : TextFile;
   j: integer;
 begin
   AssignFile(SomeTxtFile, 'c:\MyTextFile.txt') ;
   Rewrite(SomeTxtFile) ;
   for j := 0 to (-1 + Memo1.Lines.Count) do
     WriteLn(SomeTxtFile, Memo1.Lines[j]) ;
   CloseFile(SomeTxtFile) ;
 end; 
Depending on the state of the file provided to the Rewrite procedure it creates a new file (opens the file for output) with the name assigned to SomeTextFile. If a file with the same name already exists it is deleted and a new empty file is created in its place. If SomeTextFile is already open, it is first closed and then re-created. The current file position is set to the beginning of the empty file.

Note: Memo1.Lines.SaveToFile('c:\MyTextFile.txt') will do the same.

Sometimes we'll just need to add some text data to the end of an existing file. If this is the case, we'll call Append to ensure that a file is opened with write-only access with the file pointer positioned at the end of the file. Something like:

 var
   SomeTxtFile : TextFile;
 begin
   AssignFile(SomeTxtFile, 'c:\MyTextFile.txt') ;
   Append(SomeTxtFile) ;
   WriteLn(SomeTxtFile, 'New line in my text file') ;
   CloseFile(SomeTxtFile) ;
 end; 

Be Aware of Exceptions

In general, you should always use exception handling when working with files. I/O is full of surprises. Always use CloseFile in a finally block to avoid the possibility of corrupting a user's FAT. All the previous examples should be rewritten as follows:
 var
   SomeTxtFile : TextFile;
   buffer : string;
 begin
   AssignFile(SomeTxtFile, 'c:\MyTextFile.txt') ;
   try
    Reset(SomeTxtFile) ;
    ReadLn(SomeTxtFile, buffer) ;
   finally
    CloseFile(SomeTxtFile) ;
   end;
 end; 

Manipulating with Structured Files

Delphi has the ability to handle both ASCII files and files that hold binary data. Here are the techniques for working with typed and untyped (binary) files.

©2014 About.com. All rights reserved.