home *** CD-ROM | disk | FTP | other *** search
/ C!T ROM 2 / ctrom_ii_b.zip / ctrom_ii_b / PROGRAM / PASCAL / PASTUT34 / FILES.TXT < prev    next >
File List  |  1993-02-16  |  10KB  |  273 lines

  1.                      TURBO PASCAL FILES.
  2.                      ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
  3.  
  4.  There are three basic kinds of file:- text, typed and untyped. 
  5.  
  6.  Each item of recorded data in a text file is in the form of an ASCII
  7.  character, each of which occupies one byte.  Each item of a typed file is
  8.  stored as that type and occupies a number of bytes according to the type (e.g.
  9.  6 bytes for reals, two bytes for integers).  Untyped files are a direct copy
  10.  of the values stored in RAM and can therefore be read into any data type. 
  11.  
  12.  TEXT FILES. 
  13.  
  14.  Text files consist of lines of text that are terminated by CR/LF.  They may
  15.  contain characters, words and sentences.  CR/LF (ASCII codes 13 and 10) is a
  16.  delimiter, which marks the end of some element, which with text files is the
  17.  end of a line. Control and Z (ASCII code 26) marks the end of the file. 
  18.  
  19.  Text file identifiers must be declared with the reserved word TEXT. 
  20.  e.g.  Var 
  21.          TxtFile  :  Text; 
  22.  
  23.  This text file of the program must be assigned to a disk file, as follows: 
  24.  
  25.       Assign(TxtFile,'TEXT.DAT'); 
  26.  
  27.  so that henceforth the program only refers to TxtFile.  The other file
  28.  handling commands are: 
  29.  
  30.       RESET opens the disk file and prepares it as an input file, with the 
  31.             file pointer positioned at the beginning of the file. 
  32.  
  33.       REWRITE prepares a file for output, with the pointer at the beginning of
  34.               the file.  If the file already exists, its contents are erased. 
  35.               If not, the file is created. 
  36.  
  37.       APPEND preserves the contents of the file and opens it with the pointer 
  38.              located at the end of the file. 
  39.  
  40.       CLOSE closes the file, ensuring that all data in temporary buffers is 
  41.             stored to disk ('flushing the buffer').  It also frees the DOS 
  42.             file handle (only 15 can be used at a time).  Close also updates 
  43.             the DOS file directory (size, time and date). 
  44.  
  45.  Once a text file is reset, information can be extracted with READ or READLN 
  46.  as shown below: 
  47.  
  48.       Var 
  49.         TxtFile : Text; 
  50.         s : string[80]; 
  51.       Begin 
  52.         Assign (TxtFile,'TEXT.DAT'); 
  53.         Reset(TxtFile); 
  54.         Readln(TxtFile,s); {after 80 characters, pointer skips to next line} 
  55.         Writeln(s);
  56.         Read(TxtFile,s); {leaves file pointer after last character read}
  57.         Writeln(s); 
  58.         Close(TxtFile); 
  59.       End. 
  60.  
  61.  Multiple strings may be read.  e.g.  Readln(TxtFile,s1,s2,s3); 
  62.  
  63.  With text files numbers are stored as characters (not in binary).  The integer
  64.  20545 (in binary 0101000001000001) is stored in a text file as the five
  65.  characters 2, 0, 5, 4, 5.  This is five bytes long, compared with 2 bytes for
  66.  an integer. 
  67.  
  68.  If TEXT.DAT contains: 11 27.53 6.4144900000E+02 then 
  69.  
  70.       Read(TxtFile,i);                 )    or just   Read(TxtFile,i,r1,r2); 
  71.       Read(TxtFile,r1);                ) 
  72.       Read(TxtFile,r2);                ) 
  73.  
  74.  will assign the first number to an integer variable i, the next two numbers to
  75.  real variables r1 and r2. 
  76.  
  77.  EOF is a boolean function that is true when the file pointer is at the end of
  78.  the file. 
  79.  e.g. f : Text; 
  80.            ........... 
  81.            While Not Eof(f) Do ....... 
  82.  
  83.  EOLN is true when the file pointer encounters CR or the end of file. 
  84.  
  85.  SEEKEOF returns the end-of-file status of a file. 
  86.  SEEKEOLN returns the end-of-line status of a file. 
  87.  Both have the ability to skip over the ASCII characters 0 to 32 (Control and
  88.  blank characters). 
  89.  
  90.  Data can be written to files with the WRITE or WRITELN commands as follows: 
  91.  
  92.       Name := 'Jones'; 
  93.       i := 21; 
  94.       Writeln(TxtFile,name,' ',i); 
  95.  
  96.  The format can be adjusted by adding a colon and a number after the parameter.
  97.  This specifies right justification in a field width equal to that number.
  98.  With 'reals' a second colon and number indicates the number of decimal places. 
  99.  
  100.  DISK FILES and BUFFERS. 
  101.  
  102.  Reading and writing to disk is relatively slow and involves a standard time
  103.  overhead for starting and stopping the disk drive.  Buffers are used by Turbo
  104.  Pascal so that a selected number of bytes are read in one operation.  The
  105.  default size of buffer is 128 bytes, but may be changed by the SETTEXTBUF
  106.  command.  Thus every time the program reads data from a text file, the buffer
  107.  is filled with 128 bytes (or the changed number), even though a smaller amount
  108.  of data was requested.
  109.  
  110.  e.g. 
  111.       Var 
  112.         f : Text; 
  113.         buffer : Array[1..512] of Byte; 
  114.       Begin 
  115.         Assign(f,'TEST.DAT'); 
  116.         SetTextBuf(f,buffer); {must call SetTextBuf before file is opened} 
  117.         Reset(f); 
  118.         ........... 
  119.       End. 
  120.  
  121.  When writing to a buffered file, data is sent to the output buffer until it is
  122.  filled and then all this data is sent to disk at one time.  FLUSH(f) flushes
  123.  the buffer. 
  124.  
  125.  TYPED FILES. 
  126.  
  127.  Typed files contain data of a particular type: integer, real, record, etc. 
  128.  Access is faster than with text files.  Text files are unstructured, but typed
  129.  files have a rigid structure: e.g. f : File of Real; 
  130.  
  131.  The data is stored in the same format as on RAM and by-passes the translation
  132.  and conversion process of text files and hence can transfer data directly to
  133.  and from memory.  Typed files cannot use READLN and WRITELN because they are
  134.  not arranged in lines. 
  135.  
  136.  Typed files are organised into 'records', each data item representing one
  137.  record.  The length of a record corresponds to the number of bytes required to
  138.  store the data type.  Thus a File Of Real requires 6 bytes per 'record'.
  139.  whilst a File Of Integer requires 2 bytes per 'record'.  An example of a typed
  140.  file is: 
  141.  
  142.       Var 
  143.         r : real; 
  144.         f : File Of Real; 
  145.       Begin 
  146.         ClrScr; 
  147.         Assign(f,'REAL.DAT'); 
  148.         Rewrite(f); 
  149.         r := 100.234; 
  150.         Write(f,r); 
  151.         ......... 
  152.         Reset(f); 
  153.         While Not Eof(f) Do 
  154.            Begin 
  155.              Read(f,r); 
  156.              Writeln(r:8:3); 
  157.            End; 
  158.  
  159.  
  160. STRINGS and TYPED FILES.
  161.  
  162.  Typed files can also be of string type.  If the data is ABCD, then a text file
  163.  only receives ABCD, but a STRING[10] file stores: 
  164.                             
  165.            0 1 2 3 4 5 6 7 8 9 10 
  166.            4 A B C D x x x x x x 
  167.            |    |        |  
  168.       string  string   garbage 
  169.       length 
  170.                                  
  171.  Data from typed files can be transferred directly between disk and RAM,
  172.  whereas text files waste time whilst numbers are converted into characters and
  173.  back again and strings are stripped of their length byte and any unused bytes. 
  174.  
  175.  COMPLEX TYPED FILES. 
  176.  
  177.  Just as a user can define his own data types, like records, so he can define
  178.  files of these data types. 
  179.  e.g. 
  180.       Type 
  181.          CustomerRec = Record 
  182.            Name : string[30]; 
  183.            ............ 
  184.          End; 
  185.       Var 
  186.          Customer : CustomerRec; 
  187.          CustFile : File of CustomerRec; 
  188.          ....................... 
  189.       Begin 
  190.          Assign(CustFile,'CUST.DAT'); 
  191.          ..................... 
  192.          With Customer Do 
  193.            Begin 
  194.               Name := 'John Smith'; 
  195.               ................ 
  196.               Write(CustFile,Customer); 
  197.               ................ 
  198.            End; 
  199.       End. 
  200.  
  201.  Because the file is declared as type CustomerRec, complete records can be read
  202.  or written at a time. 
  203.  
  204.  UNTYPED FILES. 
  205.  
  206.  Untyped files are especially powerful, because they make no assumption about
  207.  the structure of the data in a file.  Data can be read from an untyped file
  208.  into any data type.  Disk transfer is immediate and therefore fast.  See the
  209.  example below, which is also available on diskette (A:\FILES\COPYFILE.PAS).
  210.  Untyped files are declared as type FILE. 
  211.  Both typed and untyped files are Random access files.
  212.  
  213.  
  214.  program copyfile;  
  215.  uses crt; 
  216.  var  
  217.    sourcefile,destfile : file;                       { untyped }  
  218.    recordsread         : integer;  
  219.    buffer              : array[1..10000] of byte;  { data structure is an } 
  220.                                                    { array of bytes       }
  221.  begin 
  222.     ClrScr; 
  223.     if paramcount <> 2 then 
  224.        begin 
  225.           writeln('CopyFile [FromFile] [ToFile]');  { DOS command format } 
  226.           halt; 
  227.        end; 
  228.     Assign(Sourcefile,ParamStr(1)); 
  229.     {$I-} 
  230.     Reset(Sourcefile,1);    { Reset takes 2nd parameter, record size, 1 byte}  
  231.     if IOResult <>0 then 
  232.        begin 
  233.           writeln(ParamStr(1),' not found.'); 
  234.           halt; 
  235.        end; 
  236.     Assign(DestFile,ParamStr(2)); 
  237.     rewrite(Destfile,1);  
  238.  
  239.     writeln('. = 10000 bytes copied.'); 
  240.  
  241.     blockread(sourcefile,buffer,sizeof(buffer),recordsread);  { 4 parameters } 
  242.  {               |         |         |                   | 
  243.       file identifier      |   number of records to read | 
  244.                            |                             | 
  245.          data structure into which data is placed     count of records  
  246.                                                       actually read       } 
  247.  
  248.     while recordsread > 0 do 
  249.        begin 
  250.           write('.'); 
  251.           blockwrite(destfile,buffer,recordsread);       { 3 parameters } 
  252.  {                      |       |          | 
  253.             file identifier   data    number of records to write 
  254.                             structure                                  }
  255.  
  256.           blockread(sourcefile,buffer,sizeof(buffer),recordsread); 
  257.        end;  
  258.     close(sourcefile);  
  259.     close(destfile);   
  260.     writeln;  
  261.     write('Press ENTER...');  
  262.     readln;  
  263.  end. 
  264.  
  265.      ────────────────────────────────────────────────────────
  266.  
  267.  The DOS command is:     C>copyfile file1.txt file2.txt 
  268.  
  269.  
  270.  
  271. FILES.TXT
  272. 7.11.91
  273.