                     TURBO PASCAL FILES.
                     

 There are three basic kinds of file:- text, typed and untyped. 

 Each item of recorded data in a text file is in the form of an ASCII
 character, each of which occupies one byte.  Each item of a typed file is
 stored as that type and occupies a number of bytes according to the type (e.g.
 6 bytes for reals, two bytes for integers).  Untyped files are a direct copy
 of the values stored in RAM and can therefore be read into any data type. 

 TEXT FILES. 

 Text files consist of lines of text that are terminated by CR/LF.  They may
 contain characters, words and sentences.  CR/LF (ASCII codes 13 and 10) is a
 delimiter, which marks the end of some element, which with text files is the
 end of a line. Control and Z (ASCII code 26) marks the end of the file. 

 Text file identifiers must be declared with the reserved word TEXT. 
 e.g.  Var 
         TxtFile  :  Text; 

 This text file of the program must be assigned to a disk file, as follows: 

      Assign(TxtFile,'TEXT.DAT'); 

 so that henceforth the program only refers to TxtFile.  The other file
 handling commands are: 

      RESET opens the disk file and prepares it as an input file, with the 
            file pointer positioned at the beginning of the file. 

      REWRITE prepares a file for output, with the pointer at the beginning of
              the file.  If the file already exists, its contents are erased. 
              If not, the file is created. 

      APPEND preserves the contents of the file and opens it with the pointer 
             located at the end of the file. 

      CLOSE closes the file, ensuring that all data in temporary buffers is 
            stored to disk ('flushing the buffer').  It also frees the DOS 
            file handle (only 15 can be used at a time).  Close also updates 
            the DOS file directory (size, time and date). 

 Once a text file is reset, information can be extracted with READ or READLN 
 as shown below: 

      Var 
        TxtFile : Text; 
        s : string[80]; 
      Begin 
        Assign (TxtFile,'TEXT.DAT'); 
        Reset(TxtFile); 
        Readln(TxtFile,s); {after 80 characters, pointer skips to next line} 
        Writeln(s);
        Read(TxtFile,s); {leaves file pointer after last character read}
        Writeln(s); 
        Close(TxtFile); 
      End. 

 Multiple strings may be read.  e.g.  Readln(TxtFile,s1,s2,s3); 

 With text files numbers are stored as characters (not in binary).  The integer
 20545 (in binary 0101000001000001) is stored in a text file as the five
 characters 2, 0, 5, 4, 5.  This is five bytes long, compared with 2 bytes for
 an integer. 

 If TEXT.DAT contains: 11 27.53 6.4144900000E+02 then 

      Read(TxtFile,i);                 )    or just   Read(TxtFile,i,r1,r2); 
      Read(TxtFile,r1);                ) 
      Read(TxtFile,r2);                ) 

 will assign the first number to an integer variable i, the next two numbers to
 real variables r1 and r2. 

 EOF is a boolean function that is true when the file pointer is at the end of
 the file. 
 e.g. f : Text; 
           ........... 
           While Not Eof(f) Do ....... 

 EOLN is true when the file pointer encounters CR or the end of file. 

 SEEKEOF returns the end-of-file status of a file. 
 SEEKEOLN returns the end-of-line status of a file. 
 Both have the ability to skip over the ASCII characters 0 to 32 (Control and
 blank characters). 

 Data can be written to files with the WRITE or WRITELN commands as follows: 

      Name := 'Jones'; 
      i := 21; 
      Writeln(TxtFile,name,' ',i); 

 The format can be adjusted by adding a colon and a number after the parameter.
 This specifies right justification in a field width equal to that number.
 With 'reals' a second colon and number indicates the number of decimal places. 

 DISK FILES and BUFFERS. 

 Reading and writing to disk is relatively slow and involves a standard time
 overhead for starting and stopping the disk drive.  Buffers are used by Turbo
 Pascal so that a selected number of bytes are read in one operation.  The
 default size of buffer is 128 bytes, but may be changed by the SETTEXTBUF
 command.  Thus every time the program reads data from a text file, the buffer
 is filled with 128 bytes (or the changed number), even though a smaller amount
 of data was requested.

 e.g. 
      Var 
        f : Text; 
        buffer : Array[1..512] of Byte; 
      Begin 
        Assign(f,'TEST.DAT'); 
        SetTextBuf(f,buffer); {must call SetTextBuf before file is opened} 
        Reset(f); 
        ........... 
      End. 

 When writing to a buffered file, data is sent to the output buffer until it is
 filled and then all this data is sent to disk at one time.  FLUSH(f) flushes
 the buffer. 

 TYPED FILES. 

 Typed files contain data of a particular type: integer, real, record, etc. 
 Access is faster than with text files.  Text files are unstructured, but typed
 files have a rigid structure: e.g. f : File of Real; 

 The data is stored in the same format as on RAM and by-passes the translation
 and conversion process of text files and hence can transfer data directly to
 and from memory.  Typed files cannot use READLN and WRITELN because they are
 not arranged in lines. 

 Typed files are organised into 'records', each data item representing one
 record.  The length of a record corresponds to the number of bytes required to
 store the data type.  Thus a File Of Real requires 6 bytes per 'record'.
 whilst a File Of Integer requires 2 bytes per 'record'.  An example of a typed
 file is: 

      Var 
        r : real; 
        f : File Of Real; 
      Begin 
        ClrScr; 
        Assign(f,'REAL.DAT'); 
        Rewrite(f); 
        r := 100.234; 
        Write(f,r); 
        ......... 
        Reset(f); 
        While Not Eof(f) Do 
           Begin 
             Read(f,r); 
             Writeln(r:8:3); 
           End; 


STRINGS and TYPED FILES.

 Typed files can also be of string type.  If the data is ABCD, then a text file
 only receives ABCD, but a STRING[10] file stores: 
                            
           0 1 2 3 4 5 6 7 8 9 10 
           4 A B C D x x x x x x 
           |    |        |  
      string  string   garbage 
      length 
                                 
 Data from typed files can be transferred directly between disk and RAM,
 whereas text files waste time whilst numbers are converted into characters and
 back again and strings are stripped of their length byte and any unused bytes. 

 COMPLEX TYPED FILES. 

 Just as a user can define his own data types, like records, so he can define
 files of these data types. 
 e.g. 
      Type 
         CustomerRec = Record 
           Name : string[30]; 
           ............ 
         End; 
      Var 
         Customer : CustomerRec; 
         CustFile : File of CustomerRec; 
         ....................... 
      Begin 
         Assign(CustFile,'CUST.DAT'); 
         ..................... 
         With Customer Do 
           Begin 
              Name := 'John Smith'; 
              ................ 
              Write(CustFile,Customer); 
              ................ 
           End; 
      End. 

 Because the file is declared as type CustomerRec, complete records can be read
 or written at a time. 

 UNTYPED FILES. 

 Untyped files are especially powerful, because they make no assumption about
 the structure of the data in a file.  Data can be read from an untyped file
 into any data type.  Disk transfer is immediate and therefore fast.  See the
 example below, which is also available on diskette (A:\FILES\COPYFILE.PAS).
 Untyped files are declared as type FILE. 
 Both typed and untyped files are Random access files.


 program copyfile;  
 uses crt; 
 var  
   sourcefile,destfile : file;                       { untyped }  
   recordsread         : integer;  
   buffer              : array[1..10000] of byte;  { data structure is an } 
                                                   { array of bytes       }
 begin 
    ClrScr; 
    if paramcount <> 2 then 
       begin 
          writeln('CopyFile [FromFile] [ToFile]');  { DOS command format } 
          halt; 
       end; 
    Assign(Sourcefile,ParamStr(1)); 
    {$I-} 
    Reset(Sourcefile,1);    { Reset takes 2nd parameter, record size, 1 byte}  
    if IOResult <>0 then 
       begin 
          writeln(ParamStr(1),' not found.'); 
          halt; 
       end; 
    Assign(DestFile,ParamStr(2)); 
    rewrite(Destfile,1);  

    writeln('. = 10000 bytes copied.'); 

    blockread(sourcefile,buffer,sizeof(buffer),recordsread);  { 4 parameters } 
 {               |         |         |                   | 
      file identifier      |   number of records to read | 
                           |                             | 
         data structure into which data is placed     count of records  
                                                      actually read       } 

    while recordsread > 0 do 
       begin 
          write('.'); 
          blockwrite(destfile,buffer,recordsread);       { 3 parameters } 
 {                      |       |          | 
            file identifier   data    number of records to write 
                            structure                                  }

          blockread(sourcefile,buffer,sizeof(buffer),recordsread); 
       end;  
    close(sourcefile);  
    close(destfile);   
    writeln;  
    write('Press ENTER...');  
    readln;  
 end. 

     

 The DOS command is:     C>copyfile file1.txt file2.txt 



FILES.TXT
7.11.91
