home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / progm / pascal1.zip / CHAP11.TXT < prev    next >
Text File  |  1988-01-15  |  30KB  |  653 lines

  1.  
  2.                            CHAPTER 11 - Files
  3.  
  4.  
  5.              One of the most common operations when using a computer
  6.         is to either read from, or write to a file.  You are already
  7.         somewhat experienced in file handling from the last chapter,
  8.         because in computer terminology, the keyboard, terminal, and
  9.         printer  are all classified as files.  A file is any  serial
  10.         input  or  output device that the computer  has  access  to.
  11.         Since  it  is  serial,  only one  piece  of  information  is
  12.         available to the computer at any instant of time. This is in
  13.         contrast to an array, for example, in which all elements  of
  14.         the array are stored internally and are all available at any
  15.         time.
  16.  
  17.                            A SHORT HISTORY LESSON
  18.  
  19.              Several  years ago computers were all large  cumbersome
  20.         machines with large peripheral devices such as magnetic tape
  21.         drives,  punch card readers, paper tape readers or  punches,
  22.         etc.  It was a simple task to assign the paper tape reader a
  23.         symbol and use that symbol whenever it was necessary to read
  24.         a  paper  tape.  There was never more than one file  on  the
  25.         paper  tape being read, so it was simply read  sequentially,
  26.         and  hopefully  the  data was the desired  data.   With  the
  27.         advent of floppy disks, and hard disks, it became  practical
  28.         to  put  several files of data on one disk,  none  of  which
  29.         necessarily  had anything to do with any of the other  files
  30.         on that disk.  This led to the problem of reading the proper
  31.         file from the disk, not just reading the disk.
  32.  
  33.              Pascal  was  originally released in  1971,  before  the
  34.         introduction  of  the  compact floppy  disk.   The  original
  35.         release  of Pascal had no provision for selecting a  certain
  36.         file  from  among  the  many included  on  the  disk.   Each
  37.         compiler  writer had to overcome this deficiency and he  did
  38.         so  by defining an extension to the standard Pascal  system.
  39.         Unfortunately, all of the extensions were not the same,  and
  40.         there  are  now several ways to accomplish  this  operation.
  41.         There   are  primarily  two  ways,  one  using  the   Assign
  42.         statement, and the other using the Open statement.  They are
  43.         similar  to  each  other and they accomplish  the  same  end
  44.         result.
  45.  
  46.                           BACK TO THE PRESENT TIME
  47.  
  48.              All of the above was described to let you know that  we
  49.         will have a problem in this chapter, namely, how do we cover
  50.         all  of  the possible implementations of  Pascal  available?
  51.         The  answer is, we can't.  Most of what is covered  in  this
  52.         chapter will apply to all compilers, and all that is covered
  53.         will  apply to the TURBO Pascal compilers, versions 3.0  and
  54.         4.0.   If you are not using TURBO Pascal and  your  compiler
  55.         complains about some of the statements, it will be up to you
  56.  
  57.  
  58.                                  Page 63
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.                            CHAPTER 11 - Files
  69.  
  70.  
  71.         to dig out the details of how to do the intended operations.
  72.         If   any  one  or  more  of  these  operations   cannot   be
  73.         accomplished  with  your  compiler,  you  should   seriously
  74.         consider  purchasing a better compiler because all of  these
  75.         operations are needed in a useful Pascal environment.
  76.  
  77.                        READING AND DISPLAYING A FILE
  78.  
  79.              Examine  the file READFILE for an example of a  program
  80.         that  can read a text file from the disk.  In fact  it  will
  81.         read  itself  from  the disk and display  it  on  the  video
  82.         monitor.   The first statement in the program is the  Assign
  83.         statement.   This is TURBO Pascal's way of  selecting  which
  84.         file on the disk will be either read from or written to.  In
  85.         this case we will read from the disk.  The first argument in
  86.         the Assign statement is the device specifier similar to  Lst
  87.         used in the last chapter for the printer.  We have chosen to
  88.         use  the  name Turkey for the device identifier,  but  could
  89.         have  used  any valid identifier.  This identifier  must  be
  90.         defined  in a var declaration as a TEXT type variable.   The
  91.         next argument is the filename desired.  The filename can  be
  92.         defined as a string constant, as it is here, or as a  string
  93.         variable.
  94.  
  95.              The  "TEXT"  type is a predefined type and is  used  to
  96.         define  a file identifier.  It is predefined as a  "file  of
  97.         char", so it can only be used for a text file.  We will  see
  98.         later that there is another type of file, a binary file.
  99.  
  100.              Now that we have a file identified, it is necessary  to
  101.         prepare  it  for reading by executing a reset  statement  in
  102.         line  9.  The reset statement positions the read pointer  at
  103.         the  beginning of the file ready to read the first piece  of
  104.         information  in the file.  Once we have done that,  data  is
  105.         read from the file in the same manner as it was when reading
  106.         from the keyboard.  In this program, the input is controlled
  107.         by  the  while loop which is executed until we  exhaust  the
  108.         data in the file.
  109.  
  110.                  WHAT ARE THE "EOF" AND "EOLN" FUNCTIONS?
  111.  
  112.              The  Eof function is new and must be defined.  When  we
  113.         read  data from the file, we move closer and closer  to  the
  114.         end,  until  finally we reach the end and there is  no  more
  115.         data  to  read.   This  is  called  "end  of  file"  and  is
  116.         abbreviated  Eof.  Pascal has this function available  as  a
  117.         part  of the standard library which returns FALSE  until  we
  118.         reach the last line of the file.  When there is no more data
  119.         to read left in the file, the function Eof returns TRUE.  To
  120.         use  the function, we merely give it our file identifier  as
  121.  
  122.  
  123.  
  124.                                  Page 64
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.                            CHAPTER 11 - Files
  135.  
  136.  
  137.         an argument.  It should be clear that we will loop until  we
  138.         read all of the data available in the input file.
  139.  
  140.              The Eoln function is not used in this program but is  a
  141.         very  useful function.  If the input pointer is anywhere  in
  142.         the text file except at the end of a line, the Eoln  returns
  143.         FALSE, but at the end of a line, it returns a value of TRUE.
  144.         This  function  can therefore be used to find the end  of  a
  145.         line of text for variable length text input.
  146.  
  147.              To actually read the data, we use the Readln procedure,
  148.         giving it our identifier Turkey and the name of the variable
  149.         we want the data read into.  In this case, we read up to  80
  150.         characters into the string and if more are available, ignore
  151.         them.   You  should remember when we did this  in  the  last
  152.         chapter  from  the  keyboard input. We are  using  the  same
  153.         technique here except we are reading from a file this  time.
  154.         Since we would like to do something with the data, we output
  155.         the  line  to  the default device, the  video  monitor.   It
  156.         should be clear to you by now that the program will read the
  157.         entire file and display it on the monitor.
  158.  
  159.              Finally,  we Close the file Turkey.  It is  not  really
  160.         necessary to close the file because the system will close it
  161.         for  you automatically at program termination, but it  is  a
  162.         good  habit to get into.  It must be carefully  pointed  out
  163.         here,  that you did not do anything to the input  file,  you
  164.         only  read  it and left it intact.  You could Reset  it  and
  165.         reread it again in this same program.
  166.  
  167.              Compile and run this program to see if it does what you
  168.         expect it to do.
  169.  
  170.                         A PROGRAM TO READ ANY FILE
  171.  
  172.              Examine the next program READDISP for an improved  file
  173.         reading  program.  This is very similar except that it  asks
  174.         you for the name of the file that you desire to display, and
  175.         enters   the   name  into  a  12  character   string   named
  176.         Name_Of_File_To_Input.   This  is then used  in  the  Assign
  177.         statement  to  select the file to be read, and the  file  is
  178.         reset as before.  Lines 15 through 18 display a header,  and
  179.         from that point on, the program is identical to the last one
  180.         with a few small additions.  In order to demonstrate the use
  181.         of a function within the Writeln specification, the  program
  182.         calls  for  the length of the input string in  line  23  and
  183.         displays it before each line.  The lines are counted as they
  184.         are  read and displayed, and the line count is displayed  at
  185.         the end of the listing.
  186.  
  187.  
  188.  
  189.  
  190.                                  Page 65
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.                            CHAPTER 11 - Files
  201.  
  202.  
  203.              You  should  be able to see clearly how each  of  these
  204.         operations  is accomplished.  Compile and run this  program,
  205.         entering  any  filename  we have used so  far  (be  sure  to
  206.         include the .PAS extension).  After a successful run,  enter
  207.         a nonexistent filename and see the I/O error.
  208.  
  209.                        HOW TO COPY A FILE (SORT OF)
  210.  
  211.              Examine  the  file  READSTOR for  an  example  of  both
  212.         reading  from  a file and writing to another one.   In  this
  213.         program  we  request an operator input for the  filename  to
  214.         read,  after which we Assign the name to the file and  Reset
  215.         it.  When we reset the file however, we go to a bit of extra
  216.         trouble to assure that the file actually exists.
  217.  
  218.              Suppose we input a filename, and the file did not exist
  219.         because the file was actually missing, or because we entered
  220.         the  filename  wrong.  Without the extra effort,  the  TURBO
  221.         Pascal  runtime system would indicate a run-time error,  and
  222.         terminate the program returning us to the operating  system.
  223.         In  order to make a program easier to use, it would be  nice
  224.         to tell the operator that the file didn't exist and give him
  225.         the  opportunity to try again with another file  name.   The
  226.         method  given  in lines 16 through 20 of this  program  will
  227.         allow you to do just that.
  228.  
  229.                          USING A COMPILER DIRECTIVE
  230.  
  231.              First  you must disable the built in TURBO  Pascal  I/O
  232.         checking  by  inserting the compiler directive in  line  16.
  233.         This  tells  the system to ignore any I/O errors  from  this
  234.         point on and if the file doesn't exist, the system will  not
  235.         abort  when  you attempt to reset it in  line  17.   Another
  236.         compiler  directive  is  given  in line  18  to  enable  I/O
  237.         checking again for the remainder of the program.
  238.  
  239.                         WE DO OUR OWN FILE CHECKING
  240.  
  241.              If  the  file didn't exist and could not  therefore  be
  242.         reset, we have a problem because the program thinks the file
  243.         is  available for use but it actually  isn't.   Fortunately,
  244.         TURBO Pascal has a built in variable, named "IOResult", that
  245.         informs  us of the result of each I/O operation.   Following
  246.         any  I/O operation, if this variable contains the  value  of
  247.         zero, the I/O operation was correct, and if it contains  any
  248.         other  value, the operation had some sort of error.  In  our
  249.         case,  we  simply compare it to zero to generate  a  boolean
  250.         value,  then  based on the boolean value we either  give  an
  251.         error  message  and  stop,  or  perform  the  desired   file
  252.         operations.
  253.  
  254.  
  255.  
  256.                                  Page 66
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.                            CHAPTER 11 - Files
  267.  
  268.  
  269.              It would be good programming practice to check all file
  270.         openings  in  this manner to allow the operator  to  recover
  271.         from a simple oversight or spelling error.
  272.  
  273.              If  the  file  was opened properly,  then  in  line  21
  274.         through  24  we request a different filename  to  write  to,
  275.         which is assigned to a different identifier.  Note that  the
  276.         output file is not checked for a valid opening as it  should
  277.         be.   The  statement in line 24 is new to  us,  the  Rewrite
  278.         statement.  This name apparently comes from the words  REset
  279.         for  WRITEing  because  that is exactly what  it  does.   It
  280.         clears  the  entire file of any prior data and  prepares  to
  281.         write  into the very beginning of the file.  Each  time  you
  282.         write into it, the file grows by the amount of the new  data
  283.         written.
  284.  
  285.              Once  the identifier has been defined, and the  Rewrite
  286.         has  been  executed,  writing to the file  is  identical  to
  287.         writing  to the display with the addition of the  identifier
  288.         being specified prior to the first output field.  With  that
  289.         in  mind,  you  should have  no  trouble  comprehending  the
  290.         operation  of the program.  This program is very similar  to
  291.         the  last, except that it numbers the lines as the  file  is
  292.         copied.   After  running the program, look in  your  default
  293.         directory for the new filename which you input when it asked
  294.         for the output filename.  Examine that file to see if it  is
  295.         truly a copy of the input file with line numbers added.
  296.  
  297.              One word of caution.  If you used an existing  filename
  298.         for  the  output  file, the file was  overwritten,  and  the
  299.         original  destroyed.   In that case, it was  good  that  you
  300.         followed instructions at the beginning of this tutorial  and
  301.         made  a working copy of the distribution disk.  You  did  do
  302.         that, didn't you?
  303.  
  304.              Compile  and run this program two different ways,  once
  305.         with  a valid input filename that should run  properly,  and
  306.         the second time with an input filename that doesn't exist to
  307.         prove   to  yourself  that  the  test  actually  does   work
  308.         correctly.
  309.  
  310.                    HOW TO READ INTEGER DATA FROM A FILE
  311.  
  312.              It  is  well and good to be able to read  text  from  a
  313.         file, but now we would like to read other forms of data from
  314.         a  file.  First we will look at an example program  to  read
  315.         data  from  a text file, then later we will see  an  example
  316.         program that reads from a binary file.
  317.  
  318.              Examine the program READINTS for an example of  reading
  319.         data  from a text file.  A text file is an ASCII  file  that
  320.  
  321.  
  322.                                  Page 67
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.                            CHAPTER 11 - Files
  333.  
  334.  
  335.         can be read by a text editor, printed, displayed, or in some
  336.         cases,  compiled and executed.  It is simply a file made  up
  337.         of  a  long string of char type data, and  usually  includes
  338.         linefeeds, carriage returns, and blanks for neat formatting.
  339.         Nearly  every  file on the Tutorial disk you  received  with
  340.         this  package is a text file.  One notable exception is  the
  341.         file named LIST.COM, which is an executable program file.
  342.  
  343.              The  example  program has nothing new,  you  have  seen
  344.         everything in it before.  We have an assignment, followed by
  345.         a reset of our file, followed by four read and write  loops.
  346.         Each of the loops has a subtle difference to illustrate  the
  347.         Read  and Readln statements.  Notice that the same  file  is
  348.         used  for  reading four times with a Reset  prior  to  each,
  349.         illustrating   the  nondestructive  read  mentioned  a   few
  350.         paragraphs ago.
  351.  
  352.              The  file we will be using is named INTDATA.TXT and  is
  353.         on  your disk.  You could display it at this time using  the
  354.         program  READDISP  we covered recently.  Notice that  it  is
  355.         simply  composed  of  the integer values  from  101  to  148
  356.         arranged four to a line with a couple of spaces between each
  357.         for  separation and a neat appearance.  The important  thing
  358.         to remember is that there are four data points per line.
  359.  
  360.                   READ AND READLN ARE SLIGHTLY DIFFERENT
  361.  
  362.              As  variables  are read in with either  procedure,  the
  363.         input  file  is scanned for the variables  using  blanks  as
  364.         delimiters.  If there are not enough data points on one line
  365.         to satisfy the arguments in the input list, the next line is
  366.         searched  also, and the next, etc.  Finally when all of  the
  367.         arguments  in  the  input list are satisfied,  the  Read  is
  368.         complete, but the Readln is not.  If it is a Read procedure,
  369.         the input pointer is left at that point in the file, but  if
  370.         it  is a Readln procedure, the input pointer is advanced  to
  371.         the  beginning of the next line.  The next paragraph  should
  372.         clear that up for you.
  373.  
  374.              The  input data file INTDATA.TXT has four  data  points
  375.         per  line  but the first loop in  the  program  READINTS.PAS
  376.         requests  only three each time through the loop.  The  first
  377.         time  through,  it reads the values 101, 102, and  103,  and
  378.         displays those values, leaving the input pointer just  prior
  379.         to  the 104, because it is a Read procedure.  The next  time
  380.         through,  it reads the value 104, advances to the next  line
  381.         and reads the values 105, and 106, leaving the pointer  just
  382.         prior to the 107.  This continues until the 5 passes through
  383.         the loop are completed.
  384.  
  385.  
  386.  
  387.  
  388.                                  Page 68
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398.                            CHAPTER 11 - Files
  399.  
  400.  
  401.              The  loop  in  lines 19 through 22  contains  a  Readln
  402.         procedure  and also reads the values 101, 102, and 103,  but
  403.         when  the  input parameter list is satisfied, it  moves  the
  404.         pointer  to the beginning of the next line, leaving it  just
  405.         before  the  105.  The values are printed out and  the  next
  406.         time  we come to the Readln, we read the 105, 106, and  107,
  407.         and the pointer is moved to the beginning of the next  line.
  408.         It  would  be  good  to  run the  program  now  to  see  the
  409.         difference in output data for the two loops.  Remember  that
  410.         the  only  difference is that the first loop uses  the  Read
  411.         procedure, and the second uses the Readln procedure.
  412.  
  413.              When  you come back to the program again,  observe  the
  414.         last two loops, which operate much like the first two except
  415.         that there are now five requested integer variables, and the
  416.         input  file  still  only  has four per  line.   This  is  no
  417.         problem.   Both input procedures will simply read the  first
  418.         four  in the first line, advance to the second line for  its
  419.         required  fifth  input, and each will do its  own  operation
  420.         next.  The Read procedure will leave the input pointer  just
  421.         before  the  second data point of the second line,  and  the
  422.         Readln  will advance the input pointer to the  beginning  of
  423.         the  third  line.   Run this program and  observe  the  four
  424.         output fields to see an illustration of these principles.
  425.  
  426.                 NOW TO READ SOME REAL VARIABLES FROM A FILE
  427.  
  428.              By whatever method you desire, take a look at the  file
  429.         named  REALDATA.TXT supplied on your Pascal  Tutorial  disk.
  430.         You  will see 8 lines of what appears to be scrambled  data,
  431.         but it is good data that Pascal can read.  Notice especially
  432.         line  4  which has some data missing, and line 6  which  has
  433.         some extra data.
  434.  
  435.              Examine the program file READDATA which will be used to
  436.         illustrate the method of reading real type data.  Everything
  437.         should be familiar to you, since there is nothing new  here.
  438.         The Readln statement is requesting one integer variable, and
  439.         three  real variables, which is what most of the input  file
  440.         contained.   When we come to the fourth line, there are  not
  441.         enough  data points available, so the first two data  points
  442.         of  the  next  line are read to  complete  the  fourth  pass
  443.         through the loop.  Since the file pointer is advanced to the
  444.         beginning   of   the  next  line,   we   are   automatically
  445.         synchronized with the data again.  When we come to the sixth
  446.         line, the last two data points are simply ignored.  Run  the
  447.         program to see if the results are as you would predict.
  448.  
  449.              If a Read were substituted for the Readln in line 14 of
  450.         the  program, the file pointer would not be advanced to  the
  451.         beginning of line 6, after the fourth pass through the loop.
  452.  
  453.  
  454.                                  Page 69
  455.  
  456.  
  457.  
  458.  
  459.  
  460.  
  461.  
  462.  
  463.  
  464.                            CHAPTER 11 - Files
  465.  
  466.  
  467.         The next attempt to read would result in trying to read  the
  468.         value  0.0006  as  an integer, and a run  time  error  would
  469.         result.   Modify  the program, substituting a Read  for  the
  470.         Readln in line 14, and see if this is not true.
  471.  
  472.              It should be pointed out that TURBO Pascal 4.0 requires
  473.         a digit both before and after the decimal point in all  data
  474.         that  is  to  be read in as real type data  or  it  will  be
  475.         flagged as a run-time error and the program will be  halted.
  476.         The digits can be zero as they are in several places in  the
  477.         example file but they must be there.  If you are using TURBO
  478.         Pascal  3.0,  the  leading  and  trailing  digits  are   not
  479.         required.
  480.  
  481.              That is all there is to reading and writing text files.
  482.         If  you  learn the necessities, you will  not  be  stumbling
  483.         around   in   the  area  of  input/output  which   is   very
  484.         intimidating to many people.  Remember to Assign, then Reset
  485.         before  reading,  Rewrite before writing, and  Close  before
  486.         quitting.   It is of the utmost importance to close  a  file
  487.         you  have been writing to before quitting to write the  last
  488.         few buffers to the file, but it is not as important to close
  489.         read  files unless you are using a lot of them, as there  is
  490.         an  implementation dependent limit of how many files can  be
  491.         open at once.  It is possible to read from a file, close it,
  492.         reopen it, and write to it in one program.  You can reuse  a
  493.         file  as  often as you desire in a program, but  you  cannot
  494.         read from and write into a file at the same time.
  495.  
  496.                       NOW FOR BINARY INPUT AND OUTPUT
  497.  
  498.              Examine the file BINOUT for an example of writing  data
  499.         to  a file in binary form.  First there is a record  defined
  500.         in  the  type declaration part composed of  three  different
  501.         variable types.  In the var part, Output_File is defined  as
  502.         a  "file  of  Dat_Rec", the  record  defined  earlier.   The
  503.         variable Dog_Food is then defined as an array of the record,
  504.         and a simple variable is defined.
  505.  
  506.              Any  file assigned a type of TEXT, which is a "file  of
  507.         char", is a text file.  A text file can be read and modified
  508.         with  a text editor, printed out, displayed on the  monitor,
  509.         etc. If a file is defined with any other definition, it will
  510.         be  a  binary  file and will be in  an  internal  format  as
  511.         defined by the Pascal compiler.  Attempting to display  such
  512.         a file will result in very strange looking gibberish on  the
  513.         monitor.
  514.  
  515.              When we get to the program, the output file is assigned
  516.         a name in line 15, and a Rewrite is performed on it to reset
  517.         the  input pointer to the beginning of the file,  empty  the
  518.  
  519.  
  520.                                  Page 70
  521.  
  522.  
  523.  
  524.  
  525.  
  526.  
  527.  
  528.  
  529.  
  530.                            CHAPTER 11 - Files
  531.  
  532.  
  533.         file,  and  prepare for writing data into it.  The  loop  in
  534.         lines  18 through 22 simply assigns nonsense data to all  of
  535.         the variables in the 20 records so we have something to work
  536.         with.
  537.  
  538.              We write a message to the display that we are ready  to
  539.         start outputting data, and we output the data one record  at
  540.         a  time with the standard Write statement.  A  few  cautions
  541.         are  in order here.  The output file can be defined  as  any
  542.         simple variable type, integer, byte, real, or a record,  but
  543.         the  types cannot be mixed.  The record itself however,  can
  544.         be  any  combination  of data  including  other  records  if
  545.         desired,  but  any  file can only have one  type  of  record
  546.         written to it.
  547.  
  548.              A Writeln statement is illegal when writing to a binary
  549.         file  because a binary file is not line oriented.   A  Write
  550.         statement is limited to one output field per statement.   It
  551.         is a simple matter to put one Write statement in the program
  552.         for each variable you wish to write out to the file.  It  is
  553.         important to Close the file when you are finished writing to
  554.         it.
  555.  
  556.                            WHY USE A BINARY FILE
  557.  
  558.              A  binary  file written by a Pascal program  cannot  be
  559.         read  by  a  word  processor, a text  editor  or  any  other
  560.         application  program such as a database or spreadsheet,  and
  561.         it may not even be readable by a Pascal program compiled  by
  562.         a  different  companies  compiler because  the  actual  data
  563.         structure  is  implementation dependent.  It can't  even  be
  564.         read by a Pascal program using the same compiler unless  the
  565.         data  structure  is identical to the one used to  write  the
  566.         file.   With all these rules, it seems like a silly  way  to
  567.         output  data,  but there are advantages to  using  a  binary
  568.         output.
  569.  
  570.              A binary file uses less file space than a corresponding
  571.         text  file  because  the data is stored in  a  packed  mode.
  572.         Since all significant digits of real data are stored, it  is
  573.         more   precise  unless  you  are  careful  to   output   all
  574.         significant  data to the corresponding TEXT file.   Finally,
  575.         since the binary data does not require formatting into ASCII
  576.         characters,  it will be considerably faster than  outputting
  577.         it  in TEXT format.  When you run this example  program,  it
  578.         will create the file KIBBLES.BIT, and put 20 records in  it.
  579.         Return  to  DOS  and  look for  this  file  and  verify  its
  580.         existence.   If  you  try to TYPE it,  using  the  DOS  TYPE
  581.         command, you will have a real mess, but that might be a good
  582.         exercise.
  583.  
  584.  
  585.  
  586.                                  Page 71
  587.  
  588.  
  589.  
  590.  
  591.  
  592.  
  593.  
  594.  
  595.  
  596.                            CHAPTER 11 - Files
  597.  
  598.  
  599.                            READING A BINARY FILE
  600.  
  601.              BININ is another example program that will read in  the
  602.         file  we just created.  Notice that the variables are  named
  603.         differently,  but the types are all identical to those  used
  604.         to  write  the  file.  An additional line is  found  in  the
  605.         program,  the if statement.  We must check for the  "end  of
  606.         file" marker to stop reading when we find it or Pascal  will
  607.         list  an  error and terminate operation.   Three  pieces  of
  608.         information  are written out to verify that we actually  did
  609.         read the data file in.
  610.  
  611.              Once  again,  a few rules are in order.   A  Readln  is
  612.         illegal since there are no lines in a binary file, and  only
  613.         one  variable  or  record  can be read  in  with  each  Read
  614.         statement.
  615.  
  616.             WHAT ABOUT FILE POINTERS, GET, AND PUT STATEMENTS?
  617.  
  618.              File pointers and the Get and Put procedures are a part
  619.         of  standard Pascal, but since they are redundant, they  are
  620.         not  a  part of TURBO Pascal.  The standard Read  and  Write
  621.         procedures are more flexible, more efficient, and easier  to
  622.         use.   The  use of Get and Put will not  be  illustrated  or
  623.         defined  here.   If you ever have any need  for  them,  they
  624.         should be covered in detail in your Pascal reference  manual
  625.         for the particular implementation you are using.
  626.  
  627.              Pointers will be covered in detail in the next  chapter
  628.         of this tutorial.
  629.  
  630.                            PROGRAMMING EXERCISES
  631.  
  632.         1.  Write a program to read in any text file, and display it
  633.             on  the  monitor  with line numbers and  the  number  of
  634.             characters  in each line.  Finally display the number of
  635.             lines  found  in  the file,  and  the  total  number  of
  636.             characters in the entire file.  Compare this number with
  637.             the filesize given by the DOS command DIR.
  638.  
  639.         2.  Write  a silly program that will read two text files and
  640.             display  them both on the monitor on alternating  lines.
  641.             This is the same as "shuffling" the two files  together.
  642.             Take  care  to  allow them to end  at  different  times,
  643.             inserting  blank  lines  for the  file  that  terminates
  644.             earlier.
  645.  
  646.  
  647.  
  648.  
  649.  
  650.  
  651.  
  652.                                  Page 72
  653.