home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / modula2 / mod2txt.zip / CHAP8.TXT < prev    next >
Text File  |  1987-03-25  |  26KB  |  587 lines

  1.                         Chapter 8 - Input/Output
  2.  
  3.  
  4.                           A SIMPLE OUTPUT PROGRAM
  5.  
  6.              Load  and  display the file named SIMPLOUT.MOD  for  an
  7.         example  of  the simple output functions.   This program  is
  8.         limited  to writing only to the monitor but we will  get  to
  9.         files  and printer output shortly.   We must first establish
  10.         some basic principles for use with library procedures.
  11.  
  12.              The  first line of the declaration part of the  program
  13.         imports   our  two  familiar  procedures  "WriteString"  and
  14.         "WriteLn" in the same manner we are used to.   The next line
  15.         imports every procedure in "InOut" and makes them  available
  16.         for  use in the program without specifically naming each one
  17.         in the IMPORT list.   The third line imports every procedure
  18.         from "Terminal" so that they too are available for our  use.
  19.         The  procedures that are imported explicitly can be used  in
  20.         exactly  the  same manner that we have been using  them  all
  21.         along,  simply  name  the procedure with any arguments  they
  22.         use.   The  others can only be used with a "qualifier"  that
  23.         tells which library module they come from.
  24.  
  25.              An  example is the easiest way to describe their use so
  26.         refer  to  the  program  before  you.    Line  11  uses  the
  27.         explicitly defined procedure from "InOut",  line 12 uses the
  28.         same procedure from "InOut",  and line 15 uses the procedure
  29.         of  the  same  name  from  "Terminal".   Line  11  uses  the
  30.         unqualified procedure call from "InOut", and lines 12 and 15
  31.         use the qualified method of calling the procedures from both
  32.         library modules.
  33.  
  34.             In this case,  the two procedures do the same thing, but
  35.         it is not required that procedures with the same name do the
  36.         same thing.   By adding the library module name to the front
  37.         of  the procedure name with a period between them,  we  tell
  38.         the system which of the two procedures we wish to  use.   If
  39.         we  tried to explicitly import both "WriteString" procedures
  40.         we would get a compile error,  so this is the way to use the
  41.         same name twice.
  42.  
  43.                          WHAT IS A LIBRARY MODULE?
  44.  
  45.              What  I  have  been calling a library  module  is  more
  46.         properly  termed a "module" and is the biggest benefit  that
  47.         Modula-2  enjoys over other programming languages.   This is
  48.         the  quality  that  gives  Modula-2  the  ability  to   have
  49.         separately   compiled  modules,   because  a  module  is   a
  50.         compilation  unit.   When  you  get  to  Part  III  of  this
  51.         tutorial,  you  will  learn how to write  your  own  modules
  52.         containing  your own favorite procedures,  and call them  in
  53.         any  program  in the same manner that you have been  calling
  54.         the procedures provided by your compiler writer.
  55.  
  56.  
  57.                                  Page 49
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.                         Chapter 8 - Input/Output
  68.  
  69.  
  70.  
  71.             None of the procedures you have been importing are  part
  72.         of  the  Modula-2  language,  they  are  extensions  to  the
  73.         language  provided for you by your compiler  writer.   Since
  74.         they  are not standard parts of the language,  they may vary
  75.         from compiler to compiler.  For that reason, I have tried to
  76.         use those defined by Niklaus Wirth in his definition of  the
  77.         language, and no others.
  78.  
  79.                         STUDY YOUR REFERENCE MANUAL
  80.  
  81.              This  would  be a good place for you to stop and  spend
  82.         some  time  reading  your reference  manual.   Look  up  the
  83.         section in your manual that is probably called the "Library"
  84.         and read through some of the details given there.   You will
  85.         find  that there are many things listed there that you  will
  86.         not  understand at this point,  but you will also find  many
  87.         things there that you do understand.   Each module will have
  88.         a  number of procedures that are "exported" so that you  can
  89.         "import"  them  and use them.   Each procedure will  have  a
  90.         definition  of  what arguments are required in order to  use
  91.         it.   Most of these definitions should be understandable  to
  92.         you.   One  thing you will find is that only the  "PROCEDURE
  93.         procname;"  is  given  along with the  arguments,  with  the
  94.         actual code of the procedure omitted.   We will study  about
  95.         this  in  "Part III" also.   The part that is shown  is  the
  96.         "DEFINITION   MODULE"   which   only   gives   the   calling
  97.         requirements.  The  "IMPLEMENTATION MODULE" which gives  the
  98.         actual program code of the procedure is usually not given by
  99.         compiler writers.
  100.  
  101.              As  you  study  the  library  modules,  you  will  find
  102.         procedures  to  handle strings,  variables  and  conversions
  103.         between  the two.   You will find  "mouse"  drivers,  "BIOS"
  104.         calls  to the inner workings of your operating  system,  and
  105.         many other kinds of procedures.  All of these procedures are
  106.         available  for you to use in your programs.   They have been
  107.         written,  debugged,  and  documented  for your use once  you
  108.         learn to use them.   In addition,  you will have the ability
  109.         to add to this list by creating your own modules  containing
  110.         your own procedures.
  111.  
  112.                        BACK TO THE PROGRAM "SIMPLOUT"
  113.  
  114.              Notice that in lines 13,  17,  and 22,  three different
  115.         ways  are  used to call "WriteLn",  even  though  there  are
  116.         actually  only  two procedures (that happen to do  the  same
  117.         thing).  A little time spent here will be time well spent in
  118.         preparing  for  the next few programs.   When you think  you
  119.         understand this program, compile and run it.
  120.  
  121.  
  122.  
  123.                                  Page 50
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.                         Chapter 8 - Input/Output
  134.  
  135.  
  136.                     NOW FOR SOME SIMPLE INPUT PROCEDURES
  137.  
  138.              Load  the  program  named  SIMPLIN.MOD  for  our  first
  139.         example  of a program with some data input  procedures.   In
  140.         every program we have run so far in this tutorial,  all data
  141.         has  been stored right in the program statements.   It would
  142.         be a very sad computer that did not have the ability to read
  143.         variable data in from the keyboard and files.   This program
  144.         is our first that can read from an external device,  and  it
  145.         will be limited to only the keyboard.
  146.  
  147.              This   program  is  broken  up  into  four  groups   of
  148.         statements,  each  illustrating some aspect of reading  data
  149.         from the keyboard.  This could have been four separate files
  150.         but  it  will  be  easier  to  compile  and  run  one  file.
  151.  
  152.              Beginning  with  line  14  we have an  example  of  the
  153.         "ReadString"  procedure  which  reads  characters  until  it
  154.         receives  a  space,   a  tab,   a  return,   or  some  other
  155.         nonprintable character.   This loop will read three words on
  156.         one  line,  one  word  on   each  of  three  lines,  or  any
  157.         combination to get three words of groups of printable  ASCII
  158.         characters.   After each word or group is read, it is simply
  159.         "printed" to the monitor for your inspection.
  160.  
  161.                           ONE CHARACTER AT A TIME
  162.  
  163.              The  next  group of statements is a loop  in  which  50
  164.         ASCII  characters are read in and immediately echoed out  to
  165.         the  monitor.    It  should  be  evident  to  you  that  the
  166.         characters  are  read  one at a time,  and  since  the  same
  167.         variable is used for each character,  they are not stored or
  168.         saved in any way.   In actual practice, the characters would
  169.         be  stored for whatever purpose you intend to use them  for.
  170.         When you run this part of the program, it will seem like the
  171.         computer  is simply acting like a  word  processor,  echoing
  172.         your  input back to the monitor.
  173.  
  174.                              ONE LINE AT A TIME
  175.  
  176.              The next section, beginning in line 32, reads in a full
  177.         line before writing it out to the monitor.   In this program
  178.         we  are introduced to the "EOL" which is a constant  defined
  179.         by the system for our use.  It must be imported from "InOut"
  180.         just like the procedures are,  and it is a constant that  is
  181.         equal  to that ASCII value that is returned when we hit  the
  182.         "return"  key.   It  is therefore equal to  the  End-Of-Line
  183.         character,  and that is how it got its name.   If we compare
  184.         the  input character to it,  we can determine when we get to
  185.         the End-Of-Line.   That is exactly what this loop does.   It
  186.         continues to read characters until we find an EOL,  then  it
  187.  
  188.  
  189.                                  Page 51
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.                         Chapter 8 - Input/Output
  200.  
  201.  
  202.         terminates  the  input loop and displays the line  of  data.
  203.         Notice  that  this time we do not simply read the  data  and
  204.         ignore  it but instead add it character by character to  the
  205.         ARRAY  named  "StringOfData".   Of  course,  the  next  time
  206.         through the loop we overwrite it.   The careful student will
  207.         also  notice that,  in line 45 we wrote a zero character  in
  208.         the  character  of the line just past the end of  the  line.
  209.         The  zero  is to indicate the end-of-string for  the  string
  210.         handling  procedures.   This portion of the program is easy,
  211.         but  will require a little time on your part  to  completely
  212.         dissect it.
  213.  
  214.                      READING IN SOME NUMBERS, CARDINAL
  215.  
  216.              Beginning in line 51,  we have an example of reading  6
  217.         CARDINAL numbers in a loop.   The procedure "ReadCard" will,
  218.         when  invoked  by your program,  read as many digits as  you
  219.         give it.  When it reads any character other than a 0 through
  220.         9,  it  will terminate and return the number to your calling
  221.         program.   Notice that this time all 6 numbers are read  in,
  222.         stored,  and when all are in,  they are all displayed on one
  223.         line.  This should be easy for you to decipher.
  224.  
  225.                      COMPILING AND RUNNING THIS PROGRAM
  226.  
  227.              There  is no program that you have studied here that is
  228.         as important for you to compile and run as this one is.  You
  229.         should  spend  considerable time running  this  program  and
  230.         comparing the results with the listing.   Enter some invalid
  231.         data  when  you are running the "ReadCard" portion of it  to
  232.         see what it does.  When you are running the "line at a time"
  233.         portion, try to enter more than 80 characters to see what it
  234.         will do with it.  This is a good point for you to learn what
  235.         happens when errors occur.   After you understand what  this
  236.         program does, we will proceed to file input and output.
  237.  
  238.                              FILE INPUT/OUTPUT
  239.  
  240.              Load  and  display the file named FILEIO.MOD  for  your
  241.         first  file reading and writing.   The library module  named
  242.         "InOut" has the ability to either read and write from/to the
  243.         keyboard  and monitor,  or to read and write from/to  files.
  244.         The  program  before you redirects the input and  output  to
  245.         files for an illustration of how to do it.
  246.  
  247.              Line 16 requests the operator,  namely you,  to enter a
  248.         filename  to be used for input.   There is nothing different
  249.         about  this statement than the others you have  been  using.
  250.         The  next  line  requests  the system to  open  a  file  for
  251.         inputting, and part of the procedure "OpenInput" is to go to
  252.         the  keyboard waiting for the filename to be typed  in.   So
  253.  
  254.  
  255.                                  Page 52
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.                         Chapter 8 - Input/Output
  266.  
  267.  
  268.         the  message  in line 16 is in preparation for what we  know
  269.         will  happen in line 17.   Whatever filename is typed in  is
  270.         opened for reading if it is found on the disk.  The "MOD" in
  271.         the parentheses is a default extension supplied,  (this  can
  272.         be  any extension you desire).   If no extension is supplied
  273.         by  the operator,  and if the filename does not have  a  "."
  274.         following it,  ".MOD" will be added to the filename.  If the
  275.         system can find the requested filename.extension, the "Done"
  276.         flag is made TRUE and we can test it.   In this example,  if
  277.         the flag is returned FALSE, we ask the operator to try again
  278.         until  he  finally  inputs  a filename that  exists  on  the
  279.         default disk/directory.
  280.  
  281.                          NOW TO OPEN AN OUTPUT FILE
  282.  
  283.              Once  again,  in  line 21,  we request a  filename  for
  284.         output  anticipating  the operation of the  "OpenOutput"  in
  285.         line  22.   Line 22 waits for a keyboard input of a filename
  286.         and  if the filename entered has no extension,  it adds  the
  287.         extension  ".DOG" and attempts to open the file for  writing.
  288.         When you input the filename,  adding a "." to the end of the
  289.         filename  will prevent the extension being  added.   If  the
  290.         Filename.extension  does not exist,  it will be created  for
  291.         you.  If it does exist, it's contents will be erased.
  292.  
  293.              It is nearly assured that the file will be created  and
  294.         the  "Done" flag will be supplied as TRUE,  but it would  be
  295.         good practice to check the flag anyway.  It will be apparent
  296.         when  we  get to the program on printer output,  that it  is
  297.         impossible  to  open a file with certain  names,  one  being
  298.         "PRN",   because   the   name  is   reserved   for   printer
  299.         identification and the "Done" flag will be returned FALSE.
  300.  
  301.                        HOW DO I USE THE OPENED FILES?
  302.  
  303.              Anytime  you  use  this technique to open  a  file  for
  304.         writing,  any procedure from InOut will now be redirected to
  305.         that  file.   Anytime you use this technique to open a  file
  306.         for  reading,  any procedure from InOut will access the file
  307.         named  instead of the keyboard.   In addition,  the  library
  308.         module  named  "RealInOut"  will  also  be  redirected  with
  309.         "InOut".   Any time you read or write,  instead of using the
  310.         keyboard  and  monitor,  the input and output files will  be
  311.         used.   The  input  and output will be the same  except  for
  312.         where it goes to and comes from,  and it is possible to only
  313.         open one and leave the other intact.  Thus input can be from
  314.         a file, and output can still go to the monitor.
  315.  
  316.              When  I/O is redirected, the module "Terminal" is  still
  317.         available for use with the monitor and keyboard as I/O using
  318.         this  module  can  not  be  redirected.   The  module  named
  319.  
  320.  
  321.                                  Page 53
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.                         Chapter 8 - Input/Output
  332.  
  333.  
  334.         "Terminal" does not have the flexibility of input and output
  335.         that is found in "InOut" so it is a little more difficult to
  336.         use.
  337.  
  338.           There is a major drawback when using "InOut" with the  I/O
  339.         redirected.   You  are limited to one file for input and one
  340.         file for output at one time.  Finally, this method cannot be
  341.         used  to open a "fixed" or prenamed file,  since  it  always
  342.         surveys  the  keyboard for the filename.   It will  probably
  343.         come  as  no surprise to you that all of  these  limitations
  344.         will  be overcome with another method given in the next  two
  345.         programs.
  346.  
  347.              The  program itself should be easy to follow,  once you
  348.         realize that the flag named "Done" returns TRUE when a valid
  349.         character  is found following a "Read",  and FALSE  when  an
  350.         End-Of-File  (EOF) is detected.   The "Done" flag is set  up
  351.         following  each  operation so its use is dictated  by  which
  352.         procedure  was called last.   The program simply copies  all
  353.         characters  from one file to another.   When completed,  the
  354.         two  procedures  named "CloseInput"  and  "CloseOutput"  are
  355.         called  to do just that,  to close the files and once  again
  356.         make the I/O available to the keyboard and monitor.  In this
  357.         case,  however, we immediately terminate the program without
  358.         taking advantage of the return to normal.
  359.  
  360.              Compile and run this program, being careful not to give
  361.         it  the  name  of an existing file for output,  or  it  will
  362.         overwrite  the old data in the file and copy new  data  into
  363.         it.  That is the reason for the extension "DOG".  Few people
  364.         will  have a file with that extension.   For input,  use the
  365.         present  filename (FILEIO.MOD),  for  output,  use  "STUFF",
  366.         "STUFF.",  and  "STUFF.STU",  observing  the  resulting  new
  367.         filename each time.
  368.  
  369.                           THE COMPLETE FILESYSTEM
  370.  
  371.              Load  and  display the file named VARYFILE.MOD  for  an
  372.         example  using the complete "FileSystem" module.   As stated
  373.         earlier,  Modula-2  does not have any  input/output  methods
  374.         defined  as part of the language.   This is because the  I/O
  375.         available on computers is so diverse,  there would be no way
  376.         of  defining  a method that could be used on all  computers.
  377.         To  eliminate the problem,  Niklaus Wirth simply defined  no
  378.         I/O  as  part  of the language,  but he did  suggest  a  few
  379.         standard modules to perform the basic I/O tasks.  Since they
  380.         are only suggestions,  compiler writers are not  constrained
  381.         to  follow them,  but in the interest of  portability,  most
  382.         will.   A  very limited subset of all of the procedures  are
  383.         the  only ones that will be used in the tutorial portion  of
  384.         this  course.   (A few other procedures will be used in  the
  385.  
  386.  
  387.                                  Page 54
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.                         Chapter 8 - Input/Output
  398.  
  399.  
  400.         example programs given in those chapters.)  It will be up to
  401.         you  to  see  that  the procedures are in  order  with  your
  402.         compiler,  and  where they differ,  to modify them.   A  few
  403.         notes are available for your assistance in the  COMPILER.DOC
  404.         file on your distribution disk for those compilers available
  405.         at the time of release of this tutorial.
  406.  
  407.                      BACK TO THE PROGRAM NAMED VARYFILE
  408.  
  409.              This time we IMPORT several procedures from the library
  410.         module named "FileSystem" for I/O use.   This time,  we  ask
  411.         for  the input filename and store it internally in a  string
  412.         variable.  This implies that we can also define the filename
  413.         as  a  constant that is carried in the  program,  making  it
  414.         possible to use a certain preprogrammed filename for  input.
  415.         We  use  the  procedure "Lookup" to  open  the  file.   This
  416.         procedure  uses three arguments within the parentheses,  the
  417.         first  being  the  symbolic filename which is  a  record  of
  418.         information about the file.  (We will come to records later,
  419.         don't  worry too much about it at this point.)   The  second
  420.  
  421.         argument  is the name of the file on disk we wish to access,
  422.         and  the third argument is a boolean variable  or  constant.
  423.         If it is TRUE, and the file name is not found, a new file of
  424.         that name will be created.   If it is FALSE, and the file is
  425.         not  found,  a new file will not be created,  and the record
  426.         variable  "InFile.res"  will  return  the  value  "notdone".
  427.         (That refers to one variable named "res" which is a part  of
  428.         the record "InFile".)
  429.  
  430.              Note  that the variable "InFile",  is a record composed
  431.         of many parts, but for the immediate future  we only need to
  432.         be  concerned  with  its definition.   It is  defined  as  a
  433.         variable  of type "File" which is imported from  the  module
  434.         named  "FileSystem".   Until  you study the lesson  in  this
  435.         tutorial  on records,  simply copy the method used here  for
  436.         file Input/Output.
  437.  
  438.              Once  the  file  is  opened,  you can use  any  of  the
  439.         procedures  included  in  the  "FileSystem"  module,   being
  440.         careful   to  follow  the  rules  given  in   your   library
  441.         documentation.  The remainder of the program should be self-
  442.         explanatory and will be left to your inspection.   With this
  443.         example in hand,  spend some time studying your "FileSystem"
  444.         module to become familiar with it,  then compile the program
  445.         and run it to observe its operation.
  446.  
  447.                       NOW FOR MULTIPLE FILE OPERATIONS
  448.  
  449.              Load  and  display the file named PRINTFLE.MOD  for  an
  450.         example program that uses 4 files at once,  and still writes
  451.         to the monitor.  This program is very similar to the last in
  452.  
  453.  
  454.                                  Page 55
  455.  
  456.  
  457.  
  458.  
  459.  
  460.  
  461.  
  462.  
  463.  
  464.                         Chapter 8 - Input/Output
  465.  
  466.  
  467.         that it opens one file for reading, but it opens three files
  468.         for writing.  Each of the four files has its own identifier,
  469.         a record of type "File", and each has its own filename.  The
  470.         three  output files are firmly fixed to  certain  filenames,
  471.         rather  than  ask  the operator for  names,  and  the  third
  472.         filename is a very special name,  "PRN".  This is not a file
  473.         but is the access to the printer.   Anything written to this
  474.         file  will go to your line printer,  so you should turn your
  475.         printer on in anticipation of running it.  Your compiler may
  476.         also allow a few other names such as  "LPT0",  "LPT1",  etc,
  477.         and there may be other names reserved for serial I/O such as
  478.         to  talk  to a modem,  a joystick,  etc.   You will need  to
  479.         consult  your compiler documentation for a complete list  of
  480.         special names.
  481.  
  482.  
  483.              The  program itself is very simple and similar  to  the
  484.         last  one.   A  character is read from the input  file,  and
  485.         output to the three output files and to the monitor.  In the
  486.         case of "CapFile", the character is capitalized before it is
  487.         output  simply to indicate to you that the files are  indeed
  488.         different.   Study it until you understand it,  then compile
  489.         and run it.  Look at the contents of the new files to see if
  490.         they are correct.
  491.  
  492.                    MORE NEAT THINGS WE CAN DO WITH FILES
  493.  
  494.              There  are  many more things that you can do  with  the
  495.         "FileSystem" module.   It is possible to open a file,  begin
  496.         reading until you come to a selected position, and change to
  497.         a  write file to overwrite some of the data with  new  data.
  498.         You can write to a file,  change it to a read file, reset it
  499.         to  the  beginning,  and read the data back  out.   You  can
  500.         rename a file,  or delete it.  It will be up to you to study
  501.         the  documentation for your "FileSystem" module,  and  learn
  502.         how to use it effectively.
  503.  
  504.                    YOU ARE AT A SPECIAL POINT IN MODULA-2
  505.  
  506.              With  the completion of this chapter,  you have arrived
  507.         at  a very special point in your study  of  Modula-2.   Many
  508.         people arrive at this point in a language and quit studying,
  509.         preferring  to use the language in a somewhat limited  sense
  510.         rather than to go on and learn the advanced topics.  If your
  511.         needs  are few,  you can quit here also and be well  assured
  512.         that  you can write many programs with  Modula-2.   In  fact
  513.         there will be very few times when you cannot do all that you
  514.         wish to do.  However, if you choose to go on to the advanced
  515.         topics,  you  will find that some of the programming  chores
  516.         will be greatly simplified.
  517.  
  518.  
  519.  
  520.                                  Page 56
  521.  
  522.  
  523.  
  524.  
  525.  
  526.  
  527.  
  528.  
  529.  
  530.                         Chapter 8 - Input/Output
  531.  
  532.  
  533.              Whether  you decide to go on to the advanced topics  or
  534.         not,  it  would  be wise for you to stop at this  point  and
  535.         begin  using  what you have learned to actually  write  some
  536.         programs  for  your own personal use.   Everybody  has  need
  537.         occasionally for a program to do some sort of translation of
  538.         data in a text file for example.   Write programs to do some
  539.         data shuffling from file to file changing the format in some
  540.         way.   You should be able to think up several programs  that
  541.         you would find useful.
  542.  
  543.              Spend  some  time studying and running the programs  in
  544.         the  next  chapter,  then modify them to  suit  your  needs,
  545.         building  up  a few utilities for your software  collection.
  546.         The  best way to learn to program is to program.   You  have
  547.         all  of the tools you need to get started,  so you would  do
  548.         well  to  get started.   Adding some programming  experience
  549.         will be a big help if you decide to continue your study into
  550.         the advanced features of Modula-2.
  551.  
  552.  
  553.  
  554.  
  555.  
  556.  
  557.  
  558.  
  559.  
  560.  
  561.  
  562.  
  563.  
  564.  
  565.  
  566.  
  567.  
  568.  
  569.  
  570.  
  571.  
  572.  
  573.  
  574.  
  575.  
  576.  
  577.  
  578.  
  579.  
  580.  
  581.  
  582.  
  583.  
  584.  
  585.  
  586.                                  Page 57
  587.