home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / PASCTUT1.ZIP / CHAP11.TXT < prev    next >
Encoding:
Text File  |  1986-07-14  |  25.9 KB  |  587 lines

  1.                            CHAPTER 11 - Files
  2.  
  3.  
  4.             One  of the most common operations when using a computer 
  5.  
  6.         is to either read from, or write to a file.  You are already 
  7.  
  8.         somewhat experienced in file handling from the last chapter, 
  9.  
  10.         because in computer terminology, the keyboard, terminal, and 
  11.  
  12.         printer are all classified as files.   A file is any  serial 
  13.  
  14.         input  or  output  device that the computer has  access  to.  
  15.  
  16.         Since  it  is  serial,  only one  piece  of  information  is 
  17.  
  18.         available to the computer at any instant of time. This is in 
  19.  
  20.         contrast to an array,  for example, in which all elements of 
  21.  
  22.         the array are stored internally and are all available at any 
  23.  
  24.         time.
  25.  
  26.             Several  years  ago computers were all large  cumbersome 
  27.  
  28.         machines with large peripheral devices such as magnetic tape 
  29.  
  30.         drives,  punch card readers,  paper tape readers or punches, 
  31.  
  32.         etc.  It was a simple task to assign the paper tape reader a 
  33.  
  34.         symbol and use that symbol whenever it was necessary to read 
  35.  
  36.         a  paper tape.   There was never more than one file  on  the 
  37.  
  38.         paper  tape being read,  so it was simply read sequentially, 
  39.  
  40.         and  hopefully  the data was the  desired  data.   With  the 
  41.  
  42.         advent  of  floppy  disks,  and hard disks  too,  it  became 
  43.  
  44.         practical to put several files of data on one disk,  none of 
  45.  
  46.         which  necessarily had anything to do with any of the  other 
  47.  
  48.         files on that disk.   This led to the problem of reading the 
  49.  
  50.         proper file from the disk, not just reading the disk.
  51.  
  52.             Pascal  was  originally released  in  1971,  before  the 
  53.  
  54.         introduction  of  the  compact floppy  disk.   The  original 
  55.  
  56.         release  of Pascal had no provision for selecting a  certain 
  57.  
  58.         file  from  among  the many  included  on  the  disk.   Each 
  59.  
  60.         compiler  writer had to overcome this deficiency and he  did 
  61.  
  62.         so  by defining an extension to the standard Pascal  system.  
  63.  
  64.         Unfortunately,  all of the extensions were not the same, and 
  65.  
  66.         there  are  now several ways to accomplish  this  operation.  
  67.  
  68.         There   are  primarily  two  ways,   one  using  the  ASSIGN 
  69.  
  70.         statement, and the other using the OPEN statement.  They are 
  71.  
  72.         similar  to  each  other and they accomplish  the  same  end 
  73.  
  74.         result.
  75.  
  76.             All  of the above was described to let you know that  we 
  77.  
  78.         will have a problem in this chapter, namely, how do we cover 
  79.  
  80.         all  of  the possible implementations of  Pascal  available?  
  81.  
  82.         The answer is,  we can't.   Most of what is covered in  this 
  83.  
  84.         chapter will apply to all compilers, and all that is covered 
  85.  
  86.         will  apply to the TURBO Pascal compiler.   If your compiler 
  87.  
  88.         complains about some of the statements, it will be up to you 
  89.  
  90.         to dig out the details of how to do the intended operations.  
  91.  
  92.         If there is no way to do any of these operations, you should 
  93.  
  94.         seriously  consider getting another compiler because all  of 
  95.  
  96.         these operations are needed in a useful Pascal environment.
  97.  
  98.  
  99.  
  100.  
  101.                                  Page 51
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.                            CHAPTER 11 - Files
  112.  
  113.  
  114.                        READING AND DISPLAYING A FILE
  115.  
  116.             Examine  the file READFILE for an example of  a  program 
  117.  
  118.         that  can  read a text file from the disk,  in fact it  will 
  119.  
  120.         read  itself  from  the disk and display  it  on  the  video 
  121.  
  122.         monitor.   The  first statement in the program is the ASSIGN 
  123.  
  124.         statement.   This  is TURBO Pascal's way of selecting  which 
  125.  
  126.         file on the disk will be either read from or written to.  In 
  127.  
  128.         this case we will read from the disk.  The first argument in 
  129.  
  130.         the  ASSIGN  statement is the device  specifier  similar  to 
  131.  
  132.         "lst"  used  in the last chapter for the printer.   We  have 
  133.  
  134.         chosen  to  use  "turkey",  but could have  used  any  valid 
  135.  
  136.         identifier.   This  identifier  must  be defined  in  a  VAR 
  137.  
  138.         declaration as a TEXT type variable.   The next argument  is 
  139.  
  140.         the  filename  desired.   The filename can be defined  as  a 
  141.  
  142.         string constant, as it is here, or as a string variable.
  143.  
  144.             The TEXT type is a predefined type and is used to define 
  145.  
  146.         a file identifier.  It is predefined as a "file of CHAR", so 
  147.  
  148.         it can only be used for a text file.  We will see later that 
  149.  
  150.         there is another type of file, a binary file.
  151.  
  152.             Now  that we have a file identified,  it is necessary to 
  153.  
  154.         prepare it for reading by executing a RESET statement.   The 
  155.  
  156.         reset statement positions the read pointer at the  beginning 
  157.  
  158.         of  the file ready to read the first piece of information in 
  159.  
  160.         the  file.   Once we have done that,  data is read from  the 
  161.  
  162.         file  in  the same manner as it was when  reading  from  the 
  163.  
  164.         keyboard.   In this program,  the input is controlled by the 
  165.  
  166.         WHILE  loop  which is executed until we exhaust the data  in 
  167.  
  168.         the file.
  169.  
  170.                  WHAT ARE THE "EOF" AND "EOLN" FUNCTIONS?
  171.  
  172.             The "eof" function is new and must be defined.   When we 
  173.  
  174.         read  data from the file,  we move closer and closer to  the 
  175.  
  176.         end,  until  finally we reach the end and there is  no  more 
  177.  
  178.         data  to  read.   This  is  called  "end  of  file"  and  is 
  179.  
  180.         abbreviated "eof".   Pascal has this function which is false 
  181.  
  182.         until we reach the last line of the file,  but when there is 
  183.  
  184.         no  more  data in the file to be read,  the  function  "eof" 
  185.  
  186.         becomes  true.   To use the function,  we merely give it our 
  187.  
  188.         file identifier as an argument.   It should be clear that we 
  189.  
  190.         will  loop  until we read all of the data available  in  the 
  191.  
  192.         file.
  193.  
  194.             The "eoln" function is not used in this program but is a 
  195.  
  196.         very useful function.   If the input pointer is anywhere  in 
  197.  
  198.         the  text  file  except at the end of  a  line,  the  "eoln" 
  199.  
  200.         function  is  false,  but at the end of a line,  it  becomes 
  201.  
  202.         true.   This function can therefore be used to find the  end 
  203.  
  204.  
  205.  
  206.                                  Page 52
  207.  
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216.                            CHAPTER 11 - Files
  217.  
  218.  
  219.         of  a  line  of text for variable length  text  input.   The 
  220.  
  221.         "eoln"  function is not available,  and in fact  meaningless 
  222.  
  223.         when you are reading a binary file, to be defined later.
  224.  
  225.             To actually read the data,  we use the READLN procedure, 
  226.  
  227.         giving  it  our  identifier "turkey" and  the  name  of  the 
  228.  
  229.         variable we want the data read into.   In this case, we read 
  230.  
  231.         up  to  80  characters  into  the string  and  if  more  are 
  232.  
  233.         available,  ignore  them.   Remember this from the  keyboard 
  234.  
  235.         input?  It  is  the same here.   Since we would like  to  do 
  236.  
  237.         something  with the data,  we simply output the line to  the 
  238.  
  239.         default device,  the video monitor.   It should be clear  to 
  240.  
  241.         you by now that the program will simply read the entire file 
  242.  
  243.         and display it on the monitor.
  244.  
  245.             Finally,  we CLOSE the file "turkey".   It is not really 
  246.  
  247.         necessary to close the file because the system will close it 
  248.  
  249.         for  you automatically at program termination,  but it is  a 
  250.  
  251.         good  habit to get into.   It must be carefully pointed  out 
  252.  
  253.         here,  that  you did not do anything to the input file,  you 
  254.  
  255.         only  read it and left it intact.   You could RESET  it  and 
  256.  
  257.         reread it again in this same program.   Compile and run this 
  258.  
  259.         program to see if it does what you expect it to do.
  260.  
  261.                         A PROGRAM TO READ ANY FILE
  262.  
  263.             Examine  the next program READDISP for an improved  file 
  264.  
  265.         reading  program.   This is very similar except that it asks 
  266.  
  267.         you for the name of the file that you desire to display, and 
  268.  
  269.         enters   the   name  into  a  12  character   string   named 
  270.  
  271.         "name_of_file_to_input".   This  is then used in the  ASSIGN 
  272.  
  273.         statement  to select the file to be read,  and the  file  is 
  274.  
  275.         reset  as  before.   A  header is then  displayed,  and  the 
  276.  
  277.         program  is  identical  to  the last  one  with  some  small 
  278.  
  279.         additions.   In  order to demonstrate the use of a  function 
  280.  
  281.         within the WRITELN specification,  the program calls for the 
  282.  
  283.         length of the input string and displays it before each line.  
  284.  
  285.         The  lines are counted as they are read and  displayed,  and 
  286.  
  287.         the line count is then displayed at the end of the  listing.  
  288.  
  289.         You  should  be  able  to  see clearly  how  each  of  these 
  290.  
  291.         operations is accomplished.   Compile and run this  program, 
  292.  
  293.         entering  any  filename  we  have used so far  (be  sure  to 
  294.  
  295.         include  the  .PAS).    After  a  successful  run,  enter  a 
  296.  
  297.         nonexistent filename and see the I/O error.
  298.  
  299.                        HOW TO COPY A FILE (SORT OF)
  300.  
  301.             Examine the file READSTOR for an example of both reading 
  302.  
  303.         from a file and writing to another one.   In this program we 
  304.  
  305.         request  an operator input for the filename to  read,  after 
  306.  
  307.         which we ASSIGN the name to the file and RESET it.  Next, we 
  308.  
  309.  
  310.  
  311.                                  Page 53
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.                            CHAPTER 11 - Files
  322.  
  323.  
  324.         request a different filename to write to,  which is assigned 
  325.  
  326.         to a different identifier.  The next statement is new to us, 
  327.  
  328.         the REWRITE statement.   This name apparently comes from the 
  329.  
  330.         words  REset  for WRITEing because that is exactly  what  it 
  331.  
  332.         does.   It  clears  the entire file of any  prior  data  and 
  333.  
  334.         prepares to write into the very beginning of the file.  Each 
  335.  
  336.         time you write into it,  the file grows by the amount of the 
  337.  
  338.         data written.
  339.  
  340.             Once  the identifier has been defined,  and the  REWRITE 
  341.  
  342.         has  been  executed,  writing  to the file is  identical  to 
  343.  
  344.         writing  to the display with the addition of the  identifier 
  345.  
  346.         being specified before the first output field.  With that in 
  347.  
  348.         mind, you should have no trouble comprehending the operation 
  349.  
  350.         of the program.   It is similar to the last program,  except 
  351.  
  352.         that  it  numbers the lines as the file  is  copied.   After 
  353.  
  354.         running  the  program,  look on your default  disk  for  the 
  355.  
  356.         filename  you  input when it asked for the output  filename.  
  357.  
  358.         Examine that file to see if it is truly a copy of the  input 
  359.  
  360.         file with line numbers added.   One word of caution,  if you 
  361.  
  362.         used an existing filename for the output file,  the file was 
  363.  
  364.         overwritten,  and the original destroyed.   In that case, it 
  365.  
  366.         was  good that you followed instructions at the beginning of 
  367.  
  368.         this  tutorial and made a working copy.   You did  do  that, 
  369.  
  370.         didn't you?
  371.  
  372.                    HOW TO READ INTEGER DATA FROM A FILE
  373.  
  374.             It is well and good to be able to read text from a file, 
  375.  
  376.         but now we come to the time to read data from a file.  First 
  377.  
  378.         we will read data from a text file, then later from a binary 
  379.  
  380.         file.   Examine  the  program  READINTS for  an  example  of 
  381.  
  382.         reading data from a text file.  A text file is an ASCII file 
  383.  
  384.         that can be read by a text editor, printed, displayed, or in 
  385.  
  386.         some cases, compiled and executed.  It is simply a file made 
  387.  
  388.         up of a long string of CHAR type data,  and usually includes 
  389.  
  390.         linefeeds, carriage returns, and blanks for neat formatting.  
  391.  
  392.         Nearly  every  file on the Tutorial disk you  received  with 
  393.  
  394.         this  package is a text file.   The notable exception is the 
  395.  
  396.         file named LIST.COM, which is an executable program file.
  397.  
  398.             The  example  program has nothing  new,  you  have  seen 
  399.  
  400.         everything in it before.  We have an assignment, followed by 
  401.  
  402.         a reset of our file,  followed by four read and write loops.  
  403.  
  404.         Each  of the loops has a subtle difference to illustrate the 
  405.  
  406.         READ  and READLN statements.   Notice that the same file  is 
  407.  
  408.         read in four times with a RESET prior to each,  illustrating 
  409.  
  410.         the nondestructive read mentioned a few paragraphs ago.
  411.  
  412.             The file we will be using is named INTDATA.TXT and is on 
  413.  
  414.         your  disk.   You  could display it at this time  using  the 
  415.  
  416.  
  417.  
  418.                                  Page 54
  419.  
  420.  
  421.  
  422.  
  423.  
  424.  
  425.  
  426.  
  427.  
  428.                            CHAPTER 11 - Files
  429.  
  430.  
  431.         program  READDISP  we covered recently.   Notice that it  is 
  432.  
  433.         simply  composed  of  the integer values  from  101  to  148 
  434.  
  435.         arranged four to a line with a couple of spaces between each 
  436.  
  437.         for  separation and a neat appearance.   The important thing 
  438.  
  439.         to remember is that there are four data points per line.
  440.  
  441.                   READ AND READLN ARE SLIGHTLY DIFFERENT
  442.  
  443.             As  variables  are read in with  either  procedure,  the 
  444.  
  445.         input  file  is scanned for the variables  using  blanks  as 
  446.  
  447.         delimiters.  If there are not enough data points on one line 
  448.  
  449.         to satisfy the arguments in the input list, the next line is 
  450.  
  451.         searched also,  and the next,  etc.  Finally when all of the 
  452.  
  453.         arguments  in  the  input list are satisfied,  the  READ  is 
  454.  
  455.         complete, but the READLN is not.  If it is a READ procedure, 
  456.  
  457.         the input pointer is left at that point in the file,  but if 
  458.  
  459.         it is a READLN procedure,  the input pointer is advanced  to 
  460.  
  461.         the  beginning of the next line.   The next paragraph should 
  462.  
  463.         clear that up for you.
  464.  
  465.             The input data file INTDATA.TXT has four data points per 
  466.  
  467.         line but the first loop in the program READINTS.PAS requests 
  468.  
  469.         only  three  each time through the  loop.   The  first  time 
  470.  
  471.         through, it reads the values 101, 102, and 103, and displays 
  472.  
  473.         those  values,  leaving the input pointer just prior to  the 
  474.  
  475.         104, because it is a READ procedure.  The next time through, 
  476.  
  477.         it reads the value 104,  advances to the next line and reads 
  478.  
  479.         the values 105,  and 106,  leaving the pointer just prior to 
  480.  
  481.         the 107.  This continues until the 5 passes through the loop 
  482.  
  483.         are completed.
  484.  
  485.             The next loop contains a READLN procedure and also reads 
  486.  
  487.         the values 101,  102,  and 103, but when the input parameter 
  488.  
  489.         list is satisfied,  it moves the pointer to the beginning of 
  490.  
  491.         the next line,  leaving it just before the 105.   The values 
  492.  
  493.         are printed out and the next time we come to the READLN,  we 
  494.  
  495.         read the 105,  106, and 107, and the pointer is moved to the 
  496.  
  497.         beginning  of  the next line.   It would be good to run  the 
  498.  
  499.         program now to see the difference in output data for the two 
  500.  
  501.         loops.
  502.  
  503.             When you come back to the program again, notice the last 
  504.  
  505.         two loops, which operate much like the first two except that 
  506.  
  507.         there  are  now five requested integer  variables,  and  the 
  508.  
  509.         input  file  still  only has four  per  line.   This  is  no 
  510.  
  511.         problem.   Both  input procedures will simply read the first 
  512.  
  513.         four in the first line,  advance to the second line for  its 
  514.  
  515.         required  fifth  input,  and each will do its own  operation 
  516.  
  517.         next.   The READ procedure will leave the input pointer just 
  518.  
  519.         before  the second data point of the second  line,  and  the 
  520.  
  521.         READLN  will  advance the input pointer to the beginning  of 
  522.  
  523.  
  524.  
  525.                                  Page 55
  526.  
  527.  
  528.  
  529.  
  530.  
  531.  
  532.  
  533.  
  534.  
  535.                            CHAPTER 11 - Files
  536.  
  537.  
  538.         the  third  line.   Run this program and  observe  the  four 
  539.  
  540.         output fields to see an illustration of these principles.
  541.  
  542.                 NOW TO READ SOME REAL VARIABLES FROM A FILE
  543.  
  544.             By  whatever method you desire,  take a look at the file 
  545.  
  546.         named  REALDATA.TXT supplied on your Pascal  Tutorial  disk.  
  547.  
  548.         You  will see 8 lines of what appears to be scrambled  data, 
  549.  
  550.         but it is good data that Pascal can read.  Notice especially 
  551.  
  552.         line  4  which has some data missing,  and line 6 which  has 
  553.  
  554.         some extra data.
  555.  
  556.             Examine the program file READDATA which will be used  to 
  557.  
  558.         illustrate  the  method of reading  REAL  data.   Everything 
  559.  
  560.         should be familiar to you,  since there is nothing new here.  
  561.  
  562.         Notice  the READLN statement.  It is requesting one  integer 
  563.  
  564.         variable,  and  three real variables,  which is what most of 
  565.  
  566.         the input file contained.   When we come to the fourth line, 
  567.  
  568.         there are not enough data points available, so the first two 
  569.  
  570.         data points of the next line are read to complete the fourth 
  571.  
  572.         pass.  Since the pointer is advanced to the beginning of the 
  573.  
  574.         next line,  we are automatically synchronized with the  data 
  575.  
  576.         again.   When  we come to the sixth line,  the last two data 
  577.  
  578.         points  are simply ignored.   Run the program to see if  the 
  579.  
  580.         results are as you would predict.
  581.  
  582.             If a READ were substituted for the READLN,  the  pointer 
  583.  
  584.         would not be advanced to the beginning of line 6,  after the 
  585.  
  586.         fourth  pass  through the loop.   The next attempt  to  read 
  587.  
  588.         would result in trying to read the .0006 as an INTEGER,  and 
  589.  
  590.         a  run time error would result.   Modify the program and see 
  591.  
  592.         if this is not true.
  593.  
  594.             That is all there is to reading and writing text  files.  
  595.  
  596.         If  you  learn the necessities,  you will not  be  stumbling 
  597.  
  598.         around   in   the  area  of  input/output  which   is   very 
  599.  
  600.         intimidating to many people.  Remember to ASSIGN, then RESET 
  601.  
  602.         before  reading,  REWRITE before writing,  and CLOSE  before 
  603.  
  604.         quitting.   It  is of the utmost importance to close a  file 
  605.  
  606.         you  have been writing to before quitting to write the  last 
  607.  
  608.         few  buffers to the file,  but it is not important to  close 
  609.  
  610.         read  files unless you are using a lot of them,  as there is 
  611.  
  612.         an  implementation dependent limit of how many files can  be 
  613.  
  614.         open at once.  It is possible to read from a file, close it, 
  615.  
  616.         reopen it,  and write in it in one program.  You can reuse a 
  617.  
  618.         file  as often as you desire in a program,  but  you  cannot 
  619.  
  620.         read from and write into a file at the same time.
  621.  
  622.  
  623.  
  624.  
  625.  
  626.  
  627.  
  628.                                  Page 56
  629.  
  630.  
  631.  
  632.  
  633.  
  634.  
  635.  
  636.  
  637.  
  638.                            CHAPTER 11 - Files
  639.  
  640.  
  641.                       NOW FOR BINARY INPUT AND OUTPUT
  642.  
  643.             Examine  the file BINOUT for an example of writing  data 
  644.  
  645.         to a file in binary form.   First there is a record  defined 
  646.  
  647.         in  the  type declaration part composed of  three  different 
  648.  
  649.         variable types.   In the VAR part,  "output_file" is defined 
  650.  
  651.         as  a "FILE of dat_rec",  the record defined  earlier.   The 
  652.  
  653.         variable  "dog_food"  is  then defined as an  array  of  the 
  654.  
  655.         record, and a simple variable is defined.
  656.  
  657.             Any  file assigned a type of TEXT,  which is a "FILE  of 
  658.  
  659.         CHAR", is a text file.  A text file can be read and modified 
  660.  
  661.         with a text editor,  printed out,  displayed on the monitor, 
  662.  
  663.         etc. If a file is defined with any other definition, it will 
  664.  
  665.         be  a  binary  file  and will be in an  internal  format  as 
  666.  
  667.         defined by the Pascal compiler.   Attempting to display such 
  668.  
  669.         a file will result in very strange looking gibberish on  the 
  670.  
  671.         monitor.
  672.  
  673.             When we get to the program,  the output file is assigned 
  674.  
  675.         a name,  and a REWRITE is performed on it to reset the input 
  676.  
  677.         point  to  the beginning of the file,  empty the  file,  and 
  678.  
  679.         prepare  for  writing data into it.   The next  loop  simply 
  680.  
  681.         assigns  nonsense  data to all of the variables  in  the  20 
  682.  
  683.         records so we have something to work with.
  684.  
  685.             We  finally write a message to the display that  we  are 
  686.  
  687.         ready  to start outputting data,  and we output the data one 
  688.  
  689.         record at a time with the standard WRITE statement.   A  few 
  690.  
  691.         cautions are in order here.   The output file can be defined 
  692.  
  693.         as  any simple variable type,  INTEGER,  BYTE,  REAL,  or  a 
  694.  
  695.         record, but cannot be mixed.  The record however, can be any 
  696.  
  697.         combination of data including other records, if desired, but 
  698.  
  699.         any  file  can only have one type of record written  to  it.  
  700.  
  701.         Also,  a  WRITELN  statement  is illegal when writing  to  a 
  702.  
  703.         binary file because a binary file is not line  oriented.   A 
  704.  
  705.         WRITE   statement  is  limited  to  one  output  field   per 
  706.  
  707.         statement.  It is a simple matter to put one WRITE statement 
  708.  
  709.         in  the  program for each variable you wish to write out  to 
  710.  
  711.         the  file.   It is important to CLOSE the file when you  are 
  712.  
  713.         finished writing to it.
  714.  
  715.                            WHY USE A BINARY FILE
  716.  
  717.             A binary file written by a Pascal program cannot be read 
  718.  
  719.         by a word processor,  a text editor, any application program 
  720.  
  721.         such  as a database or spreadsheet,  and it may not even  be 
  722.  
  723.         readable  by  a  Pascal  program  compiled  by  a  different 
  724.  
  725.         companies  compiler  because  the  data  is   implementation 
  726.  
  727.         dependent.   It can't even be read by a Pascal program using 
  728.  
  729.         the  correct compiler unless the data structure is identical 
  730.  
  731.  
  732.  
  733.                                  Page 57
  734.  
  735.  
  736.  
  737.  
  738.  
  739.  
  740.  
  741.  
  742.  
  743.                            CHAPTER 11 - Files
  744.  
  745.  
  746.         to the one used to write the file.  With all these rules, it 
  747.  
  748.         seems  like  a  silly way to  output  data,  but  there  are 
  749.  
  750.         advantages to using a binary output.
  751.  
  752.             A  binary file uses less file space than a corresponding 
  753.  
  754.         text  file  because  the data is stored in  a  packed  mode.  
  755.  
  756.         Since all significant digits of REAL data are stored,  it is 
  757.  
  758.         more   precise  unless  you  are  careful  to   output   all 
  759.  
  760.         significant  data to the corresponding TEXT file.   Finally, 
  761.  
  762.         since the binary data does not require formatting into ASCII 
  763.  
  764.         characters,  it will be considerably faster than  outputting 
  765.  
  766.         it  in TEXT format.   When you run the example  program,  it 
  767.  
  768.         will create the file KIBBLES.BIT,  and put 20 records in it.  
  769.  
  770.         Return  to  DOS  and  look  for this  file  and  verify  its 
  771.  
  772.         existence.   If  you try to TYPE it,  you will have  a  real 
  773.  
  774.         mess, but that might be a good exercise.
  775.  
  776.                            READING A BINARY FILE
  777.  
  778.             BININ  is another example program that will read in  the 
  779.  
  780.         file  we just created.   Notice that the variables are named 
  781.  
  782.         differently,  but the types are all identical to those  used 
  783.  
  784.         to  write  the  file.   An additional line is found  in  the 
  785.  
  786.         program,  the IF statement.   We must check for the "end  of 
  787.  
  788.         file"  marker to stop reading when we find it or Pascal will 
  789.  
  790.         list  an  error and terminate operation.   Three  pieces  of 
  791.  
  792.         information  are written out to verify that we actually  did 
  793.  
  794.         read the data file in.
  795.  
  796.             Once  again,  a  few rules are in order.   A  READLN  is 
  797.  
  798.         illegal since there are no lines in a binary file,  and only 
  799.  
  800.         one variable or record can be read in with a READ statement.
  801.  
  802.             WHAT ABOUT FILE POINTERS, GET, AND PUT STATEMENTS?
  803.  
  804.             File pointers and the GET and PUT procedures are a  part 
  805.  
  806.         of standard Pascal,  but since they are redundant,  they are 
  807.  
  808.         not  a  part of TURBO Pascal.   The standard READ and  WRITE 
  809.  
  810.         procedures are more flexible,  more efficient, and easier to 
  811.  
  812.         use.   The  use  of GET and PUT will not be  illustrated  or 
  813.  
  814.         defined here.  If you ever have any need for them, they will 
  815.  
  816.         be covered in detail in your Pascal reference manual for the 
  817.  
  818.         particular implementation you are using.
  819.  
  820.             Pointers  will be covered in detail in the next  chapter 
  821.  
  822.         of this tutorial.
  823.  
  824.  
  825.  
  826.  
  827.  
  828.  
  829.  
  830.  
  831.                                  Page 58
  832.  
  833.  
  834.  
  835.  
  836.  
  837.  
  838.  
  839.  
  840.  
  841.                            CHAPTER 11 - Files
  842.  
  843.  
  844.                            PROGRAMMING EXERCISES
  845.  
  846.         1.  Write a program to read in any text file, and display it 
  847.  
  848.             on  the  monitor  with line numbers and  the  number  of 
  849.  
  850.             characters  in each line.  Finally display the number of 
  851.  
  852.             lines  found  in  the file,  and  the  total  number  of 
  853.  
  854.             characters in the entire file.  Compare this number with 
  855.  
  856.             the filesize given by the DOS command DIR.
  857.  
  858.         2.  Write  a silly program that will read two text files and 
  859.  
  860.             display  them both on the monitor on alternating  lines. 
  861.  
  862.             This is the same as "shuffling" the two files  together. 
  863.  
  864.             Take  care  to  allow them to end  at  different  times, 
  865.  
  866.             inserting  blank  lines  for the  file  that  terminates 
  867.  
  868.             earlier.
  869.  
  870.  
  871.  
  872.  
  873.  
  874.  
  875.  
  876.  
  877.  
  878.  
  879.  
  880.  
  881.  
  882.  
  883.  
  884.  
  885.  
  886.  
  887.  
  888.  
  889.  
  890.  
  891.  
  892.  
  893.  
  894.  
  895.  
  896.  
  897.  
  898.  
  899.  
  900.  
  901.  
  902.  
  903.  
  904.  
  905.  
  906.  
  907.                                  Page 59
  908.  
  909.