home *** CD-ROM | disk | FTP | other *** search
/ Chip 1995 March / CHIP3.mdf / programm / prog1 / chap14.txt < prev    next >
Encoding:
Text File  |  1991-07-01  |  21.2 KB  |  469 lines

  1.  
  2.  
  3.  
  4.                                                        Chapter 14
  5.                                                 TEXT INPUT/OUTPUT
  6.  
  7.  
  8. ADA IS NOT BIG ON I/O
  9. _________________________________________________________________
  10.  
  11. Ada was originally designed as a language for embedded real-time
  12. systems rather than as a business oriented language, so input and
  13. output of data is a rather low priority part of the language.  It
  14. may surprise you to know that there are no input or output
  15. instructions available for use within the Ada language itself, the
  16. problem being left to the compiler writers.  However, the designers
  17. of Ada realized that if the entire topic of input and output were
  18. left to the individual compiler writers, users would be left with
  19. a mess trying to use nonstandard methods of input and output.  They
  20. exercised tremendous foresight by defining a standard external
  21. library to be used for input and output of data that must be
  22. available with all Ada compilers that pass the validation suite.
  23.  
  24. Even though the libraries are defined, the details of their actions
  25. are not precisely defined, so there is still some chance that your
  26. compiler will not behave in exactly the same way as the
  27. descriptions given here.  You will have to check the documentation
  28. packaged with your compiler to discover the exact details of input
  29. and output operation.  You may rest assured however, that the
  30. differences will only be in the smallest details.  With the
  31. preceding disclaimer, let's look at some text output procedures.
  32.  
  33.  
  34.  
  35. FORMATTED OUTPUT DATA
  36. _________________________________________________________________
  37.  
  38. If you examine the program FORMATS.ADA, you will  ===============
  39. find that it does very little other than            FORMATS.ADA
  40. illustrate the various methods of outputting      ===============
  41. formatted data to the monitor.  Several types
  42. and variables are declared, then six separate
  43. instantiations of Text_IO are declared for use with the six types
  44. used in the program.  Actually, there is nothing here that is new
  45. to you, since we have covered all of these statements before, but
  46. they are included here in order to present an example of all of
  47. them in one place.
  48.  
  49. A few comments should be made at this point.  Note the six
  50. instantiations of Text_IO packages given in lines 19 through 31.
  51. These are necessary in order to output the various types we are
  52. using in this program.  The package instantiations must be declared
  53. after the various types are declared.  One of the instantiations
  54. could be eliminated by transforming the type of the MY_INTEGER type
  55. variable to type INTEGER prior to outputting it, but this is done
  56. for illustration.  When we instantiate a package, we are actually
  57.  
  58.                                                         Page 14-1
  59.  
  60.                                    Chapter 14 - Text Input/Output
  61.  
  62. using a copy of a generic package, which will be the subject of a
  63. detailed study in part 2 of this tutorial.
  64.  
  65. After you have studied the program and understand it, compile and
  66. run it for an elaborate set of formatted output examples.  Your
  67. compiler may output the float and fixed outputs slightly
  68. differently than that given in the result of execution due to
  69. different defaults assigned by your compiler.
  70.  
  71.  
  72.  
  73. FORMATTED OUTPUT TO A FILE
  74. _________________________________________________________________
  75.  
  76. The example file named EASYOUT.ADA contains       ===============
  77. examples of how to use a file in a very easy        EASYOUT.ADA
  78. fashion.  Since there is no standard method       ===============
  79. which is used by all operating systems to name
  80. files, we cannot depend on what the rules will
  81. be in order to use Ada on any system, so we need two file names in
  82. order to write to or read from a file.  We need an internal name,
  83. which will be used by our program to refer to the file, and which
  84. follows the naming rules defined by Ada.  We also need an external
  85. name which will be used by the operating system to refer to the
  86. file and whose name follows the rules dictated by the operating
  87. system.  In addition to having the two names, we need a way to
  88. associate the names with each other, and tell the system that we
  89. wish to use the file so named.  We do all of this with the Create
  90. procedure as shown in line 14 of this program.
  91.  
  92. The variable Turkey, is the internal file name which we declared
  93. in line 10 to be of type FILE_TYPE, a type exported by the package
  94. Text_IO for just this purpose.  The "with Text_IO;" statement in
  95. line 2 makes the type FILE_TYPE available in this program.  The
  96. last argument, which is either a string constant, as it is in this
  97. case, or a string variable, gives the external name of the file we
  98. wish to create and write to.  Note carefully, that if you have a
  99. file named TEST.TXT in your default directory, it will be erased
  100. when the program is run, because this statement will erase it.  The
  101. second parameter in this procedure call, is either In_File if you
  102. intend to read from the file, or Out_File if you plan to write to
  103. it.  There is no mode using text I/O in which you can both read
  104. from and write to the same file.  In this case, we wish to write
  105. to the file, so we use the mode Out_File and create the file.
  106.  
  107.  
  108.  
  109. WRITING TO THE FILE
  110. _________________________________________________________________
  111.  
  112. Line 16 looks rather familiar to us, since it is our old friend
  113. Put_Line with an extra parameter prior to the text we wish to
  114. output.  Since Turkey is of type FILE_TYPE, the system is smart
  115. enough to know that this string will be output to the file with the
  116.  
  117.                                                         Page 14-2
  118.  
  119.                                    Chapter 14 - Text Input/Output
  120.  
  121. internal name of Turkey, and the external name of TEST.TXT, so the
  122. string will be directed there along with the "carriage return/line
  123. feed".  The next line will also be directed there, as will the 2
  124. New_Lines requested in line 18.  Line 19 does not have the filename
  125. of Turkey as a parameter, so it will be directed to the default
  126. output device, the monitor, in the same manner that it has been for
  127. all of this tutorial.
  128.  
  129.  
  130.  
  131. REDIRECTING THE OUTPUT
  132. _________________________________________________________________
  133.  
  134. Line 21 contains a call to the procedure Set_Output which will make
  135. the filename which is given as the actual parameter the default
  136. filename.  As long as this is in effect, any output without a
  137. filename will be directed to the file with the internal filename
  138. Turkey.  In order to output some data to the monitor, it can still
  139. be done, but it must be directed there by using its internal
  140. filename as a parameter, the internal filename being
  141. Standard_Output.  The group of statements given in lines 22 through
  142. 25 do essentially the same thing as those in lines 16 through 19.
  143. The default is returned to the standard output device in line 26,
  144. and the program completes normally.
  145.  
  146.  
  147.  
  148. CLOSING THE FILE
  149. _________________________________________________________________
  150.  
  151. The simple statement given in line 29 is used to close the file
  152. when we are finished with it.  The system only needs to be given
  153. the internal name of the file, since it knows the corresponding
  154. external filename.  Failure to close the file will do no harm in
  155. such a simple program, because the operating system will close it
  156. automatically, but in a large program, it would be wise to close
  157. a file when it is no longer needed.  There is an upper limit on how
  158. many files can be open at one time, and there is no sense wasting
  159. a file allocation on an unneeded file.
  160.  
  161. Be sure to compile and run this program, then check your default
  162. directory to see that you have the file named TEST.TXT in it
  163. containing the expected text.
  164.  
  165.  
  166. MULTIPLE FILES OPENED AT ONE TIME
  167. _________________________________________________________________
  168.  
  169. Examine the file MULTOUT.ADA for an example       ===============
  170. program in which we open and use 3 different        MULTOUT.ADA
  171. files plus the monitor.  The three internal       ===============
  172. filenames are declared in line 10, and all three
  173. are opened in the output mode with three
  174. different external names as shown in lines 15, 16, and 17.
  175.  
  176.                                                         Page 14-3
  177.  
  178.                                    Chapter 14 - Text Input/Output
  179.  
  180.  
  181. We execute the loop in lines 19 through 28, where we write nonsense
  182. data to the three files, output a test string to the display in
  183. line 30, and finally close all three files.  Note that we could
  184. have defined one of the files to be the default file and written
  185. to it without the filename, then used the filename for the standard
  186. output in line 30, achieving the same result.
  187.  
  188. The program is fairly simple, so after you feel you understand it,
  189. compile and run it.  After a successful run, examine the default
  190. directory to see that the three files exist and contain the
  191. expected results.  Do not erase the generated files, because we
  192. will use them as example input files in the next few programs.
  193.  
  194.  
  195.  
  196. INPUTTING CHARACTERS FROM A FILE
  197. _________________________________________________________________
  198.  
  199. Examine the file CHARIN.ADA for an example of    ================
  200. how to read CHARACTER type data from a file.  In    CHARIN.ADA
  201. this example the file name My_File is declared   ================
  202. as a FILE_TYPE variable, then used to open
  203. CHARACTS.TXT for reading since the In_File mode
  204. is used.  In this case, the file must exist, or an exception will
  205. be raised.  The file pointer is set to the beginning of the file
  206. by the Open procedure so we will read the first character in the
  207. file the first time we read from the file.
  208.  
  209. The loop in lines 17 through 21 causes us to read a character from
  210. the file and display it on the monitor each time we pass through
  211. the loop.  The function End_Of_File returns a TRUE when the next
  212. character to be read is the end of file character for your
  213. particular implementation.  We do not need to know what the end of
  214. file character is, it is up to the compiler writer to find out what
  215. it is and return a value of TRUE when the system finds it.  The
  216. entire file is therefore displayed on the monitor by this program,
  217. but with one slight deviation from what you would expect.  Because
  218. of the way Ada is defined, the end of line characters are simply
  219. ignored by the Get procedure, and since they are not read in, they
  220. cannot be output to the monitor either.  The end result is that the
  221. characters are all output in one long string with no line feeds.
  222. You will see this later when you compile and run the program.
  223.  
  224. We will continue our study in this program and see a much better
  225. way to read and display the data.
  226.  
  227.  
  228. THE RESET PROCEDURE
  229. _________________________________________________________________
  230.  
  231. The Reset procedure in line 24 resets the file named as a
  232. parameter, which simply means that the file pointer is moved to the
  233. first character once again.  The file is ready to be read in
  234.  
  235.                                                         Page 14-4
  236.  
  237.                                    Chapter 14 - Text Input/Output
  238.  
  239. another time.  The loop in lines 26 through 35 is the same as the
  240. previous loop except for the addition of another new function.
  241. When the end of a line is found, the function End_Of_Line returns
  242. a TRUE, and the program displays the special little message in line
  243. 30.  In addition to the message, the New_Line procedure is called
  244. to supply the otherwise missing end of line.  Unfortunately, we
  245. still have a problem because the End_Of_Line is actually a
  246. lookahead function and becomes TRUE when the next character in the
  247. buffer is an end of line character.  When the program is executed,
  248. we find that it does not output the last character of each line in
  249. this loop, because we never execute the else clause in the if
  250. statement for that character.  When the program is executed, you
  251. will notice that the periods at the end of the sentences are not
  252. displayed.  The next loop in this program will illustrate how to
  253. input and output the data correctly considering all of the Ada
  254. idiosyncrasies.
  255.  
  256. If you recall, we stated at the beginning of this lesson that input
  257. and output programming in Ada was at best a compromise.  Don't
  258. worry, you will be able to input or output anything you wish to
  259. very soon.
  260.  
  261.  
  262.  
  263. A CORRECT CHARACTER INPUT/OUTPUT LOOP
  264. _________________________________________________________________
  265.  
  266. The file is once again reset in line 38, and a third loop in lines
  267. 41 through 49 reads and displays the file on the monitor.  This
  268. time when we go through the loop, we recognize that the end of line
  269. is reading one character ahead in the input file, not the one we
  270. just read, and we adjust the program accordingly.  We output the
  271. character, then check for end of line, and call the New_Line
  272. procedure if we detect an end of line.  You will see, when you run
  273. it, that the last character on the line will be displayed as
  274. desired.
  275.  
  276. It should be pointed out to you that the End_Of_File is another
  277. lookahead function and must be tested after we output the last
  278. character read in.  This loop does just that, because the end of
  279. file is acted on after the character is output.
  280.  
  281. Be sure to compile and run this program.  It would be interesting
  282. to edit the input file, CHARACTS.TXT, adding a few characters, then
  283. rerunning the program to see how it handles the new data.
  284.  
  285.  
  286.  
  287. STRING INPUT AND OUTPUT
  288. _________________________________________________________________
  289.  
  290. The next example program, STRINGIN.ADA, illustrates how to read a
  291. string from an input file.  In much the same manner as for the
  292. character, the end of line character is ignored when encountered
  293.  
  294.                                                         Page 14-5
  295.  
  296.                                    Chapter 14 - Text Input/Output
  297.  
  298. in the input file.  The result is that when the  ================
  299. first loop is run, no "carriage returns" are       STRINGIN.ADA
  300. issued and the text is all run together.  The    ================
  301. second loop, however, uses the lookahead method
  302. and calls the New_Line procedure when it detects
  303. the end of line in the input stream.
  304. The big difference in this program and the last is the fact that
  305. a STRING type variable is used for input here and a CHARACTER type
  306. variable was used in the last one.
  307.  
  308.  
  309. I PLAYED A TRICK ON YOU
  310. _________________________________________________________________
  311.  
  312. This program has a bit of a trick in it.  The input file contains
  313. an exact multiple of 10 characters in each line, and the input
  314. variable, of type STRING, has ten characters in it.  There is
  315. therefore no real problem in reading into the ten character string,
  316. but if the lines were not exact multiples of ten characters, we
  317. would have gotten an input error.  If you remember how difficult
  318. the STRING variable was to work with when we studied strings, you
  319. will understand that we have the same problem here.  When we get
  320. to chapter 16 with its example programs, we will see how we can
  321. greatly improve the string capability with an add-on library of
  322. dynamic string routines.  Until then, simply realize that string
  323. input is possible, but don't get too excited about it.
  324.  
  325. Be sure to compile and run this program, then add a few characters
  326. to some of the lines in the file named CHARACTS.TXT to see the
  327. result of trying to read in an odd group of extra characters.
  328.  
  329.  
  330. NOW TO READ IN SOME NUMBERS
  331. _________________________________________________________________
  332.  
  333. Computers are very good at working with numbers,  ===============
  334. and in order to work with them, we must have a       INTIN.ADA
  335. way to get them into the computer.  The example   ===============
  336. file INTIN.ADA illustrates how to read INTEGER
  337. type data into the computer from a file.  Much
  338. like the last two programs, we open a file for reading, and begin
  339. executing a loop where we read an INTEGER type number as input each
  340. time through the loop.  We are reading an INTEGER type number
  341. because that is the type of Index, the second actual parameter in
  342. the Get procedure call.  Since it is an INTEGER type number, the
  343. system will begin scanning until it finds a valid INTEGER type
  344. number terminated by a space.  It will then return that number, in
  345. the computer's internal format, assigned to the variable we
  346. supplied to it, and make it available for our use like any other
  347. INTEGER type variable.  If any data other than INTEGER type data
  348. were to be encountered in the input file before it found a valid
  349. INTEGER value, an exception would be raised, and the program would
  350. be terminated.
  351.  
  352.  
  353.                                                         Page 14-6
  354.  
  355.                                    Chapter 14 - Text Input/Output
  356.  
  357. The loop continues in much the same manner as the last two programs
  358. until it detects the end of file, at which time it stops.  There
  359. is one other slight difference when using numeric inputs, the
  360. system does not read over the end of line character, because it
  361. doesn't know how to get across it.  It gets stuck trying to read
  362. the next data point but never gets to it because the end of line
  363. is blocking it.  When the end of line is detected, or at any other
  364. time, you can issue the function Skip_Line which tells it to go to
  365. the beginning of the next line for its next input data.  At any
  366. time then, you can begin reading on the next line for your next
  367. data point.
  368.  
  369. Be sure to compile and run this program, then change the spacing
  370. of some of the numbers in the input file to see that they are read
  371. in with a free form spacing.
  372.  
  373.  
  374. WHAT ABOUT FLOATING POINT DATA INPUTS?
  375. _________________________________________________________________
  376.  
  377. Once you understand the method of reading INTEGER type data in, you
  378. can read any other types in by using the same methods.  Just
  379. remember that the data you read in must be of the same type as the
  380. variable into which it is being read and you will have little
  381. difficulty.
  382.  
  383.  
  384. WHAT ABOUT READING FROM THE KEYBOARD?
  385. _________________________________________________________________
  386.  
  387. Reading from the keyboard is similar to reading from a file, except
  388. that it uses the predefined internal filename, Standard_Input, and
  389. if you omit a filename reference, the input will come from the
  390. keyboard.  It should be pointed out that the operating system will
  391. probably buffer the input data automatically and give it to your
  392. program as a complete string when you hit the return key.  It will
  393. allow you to enter a complete string of as many integer values as
  394. you desire, and when you hit the return, the entire string will be
  395. input and processed.
  396.  
  397. Modify the last file to read from the keyboard, and enter some
  398. numbers after compiling and running it.  Note that there is no
  399. End_Of_File when reading from the keyboard.  Use a loop with a
  400. fixed number of passes, or use an infinite loop and quit when a
  401. certain number is input such as -1.
  402.  
  403. HOW DO WE PRINT DATA ON THE PRINTER?
  404. _________________________________________________________________
  405.  
  406. The program named PRINTOUT.ADA illustrates how   ================
  407. to send data to your printer.  The only            PRINTOUT.ADA
  408. difference in sending data to the printer, from  ================
  409. sending data to a disk file, is the use of the
  410. predefined name LPT1 for the external file name.
  411.  
  412.                                                         Page 14-7
  413.  
  414.                                    Chapter 14 - Text Input/Output
  415.  
  416. This file is defined as the printer rather than a file, and as you
  417. can see from the program, it operates no differently than any other
  418. file.
  419.  
  420. Other names may be applicable for the printer also, such as PRN,
  421. COM1, etc.  Check your documentation for the predefined names that
  422. mimic a file.
  423.  
  424. Be sure your printer is on line, then compile and run this program.
  425.  
  426.  
  427. TEXT IO PRAGMAS
  428. _________________________________________________________________
  429.  
  430. A pragma is a suggestion to the compiler and can be ignored by any
  431. implementation according to the definition of Ada.  Refer to
  432. appendix B of the LRM for the definition of the predefined pragmas
  433. LIST and PAGE.  Since these may not be available with any
  434. particular compiler, their use should be discouraged in order to
  435. attain a high degree of program portability.
  436.  
  437.  
  438. PAGE AND LINE CONTROL SUBPROGRAMS
  439. _________________________________________________________________
  440.  
  441. Refer to the definition of Text_IO in the LRM for a large
  442. assortment of page and line control subprograms for use with text
  443. I/O.  These are required to be available with every compiler, so
  444. you should study them to gain an insight into the page and line
  445. control subprograms available to you in any Ada program.  There
  446. should not be anything too difficult for you to understand in these
  447. definitions.
  448.  
  449.  
  450. PROGRAMMING EXERCISES
  451. _________________________________________________________________
  452.  
  453. 1.   Modify CHARIN.ADA to read from the keyboard and echo data to
  454.      the monitor until a Q is entered.
  455.  
  456. 2.   Write a program named FLOATIN.ADA that reads floating point
  457.      data from a file and displays it.  There is no standard on
  458.      floating point input.  Some compilers require a decimal point
  459.      with at least one digit before and after the decimal point.
  460.      Some may not even require a decimal point but will supply it
  461.      to an input that looks like an INTEGER value.  Experiment and
  462.      determine the characteristics of your compiler.
  463.  
  464.  
  465.  
  466.  
  467.  
  468.  
  469.                                                         Page 14-8