home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / progm / m2t-1.zip / CHAP08.TXT < prev    next >
Text File  |  1989-01-18  |  26KB  |  588 lines

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