home *** CD-ROM | disk | FTP | other *** search
/ Solo Programadores 22 / SOLO_22.iso / docs / lovelace / lesson9.les < prev    next >
Encoding:
Text File  |  1995-11-21  |  9.6 KB  |  262 lines

  1. <COMMENT This is a lesson file for the Lovelace Ada tutorial>
  2. <COMMENT A program called genlesson is used to transform this file into a set>
  3. <COMMENT of useful HTML files for use by Mosaic & other WWW browsers.>
  4.  
  5. <COMMENT  Edit the following lines. >
  6. <TUTOR NAME="Lovelace">
  7. <LESSON NUMBER=9>
  8. <AUTHOR NAME="David A. Wheeler" EMAIL="wheeler@ida.org">
  9. <AUTHOR ADDRESS="<A HREF="dwheeler.htm">David A. Wheeler (wheeler@ida.org)</A>">
  10. <COMMENT $Id: lesson9.les,v 1.3 1995/05/17 21:25:18 wheeler Exp $ >
  11.  
  12. <COMMENT  You'll probably want to uncomment and edit these lines: >
  13. <COMMENT  <PREVIOUS_LESSON LOCATION="URL_of_directory/" >
  14. <COMMENT  <NEXT_LESSON LOCATION="URL_of_directory/" >
  15.  
  16. <COMMENT A lesson is divided into 1 or more "sections".>
  17. <COMMENT Each section has a title; SECTION starts a new section.>
  18.  
  19. <SECTION NAME="Simple Text File Input/Output">
  20. Throughout this tutorial we've been using selected input/output
  21. subprograms, mainly from the predefined package Text_IO.
  22. Now it's time to learn how to use more capabilities of Text_IO,
  23. especially how to read and write text files.
  24. <P>
  25. Operating system files are represented in Text_IO by a type called,
  26. reasonably enough, File_Type.
  27. All operations on files operate on objects of type File_Type.
  28. The default for inputting operations (such as Get)
  29. is Current_Input (Current_Input is of type File_Type), while the default for
  30. all outputting operations (such as Put and Put_Line) is Current_Output
  31. (which also has type File_Type).
  32. <P>
  33. Before a text file can be read or written it must be either
  34. opened or created.
  35. There are two basic procedures in Text_IO, called, naturally enough,
  36. Open and Create.
  37. Open opens an existing file, while Create creates a new file
  38. (eliminating the original file) and then opens it.
  39. Before you stop your program you should close all the files you've
  40. opened; the Close procedure is used to do that.
  41. Here are their definitions:
  42. <P>
  43. <PRE>
  44.   procedure Create (File : in out File_Type;
  45.                     Mode : in File_Mode := Out_File;
  46.                     Name : in String    := "";
  47.                     Form : in String    := "");
  48.  
  49.   procedure Open   (File : in out File_Type;
  50.                     Mode : in File_Mode;
  51.                     Name : in String;
  52.                     Form : in String := "");
  53.  
  54.   procedure Close  (File : in out File_Type);
  55. </PRE>
  56. <P>
  57. The ``Mode'' can be In_File (an input file),
  58. Out_File (an output file), or
  59. Append_File (an output file appending after existing text).
  60. The ``Form'' parameter is optional, and is used to provide
  61. operating-system-specific information if it's necessary.
  62. <P>
  63. All the Get and Put subprograms can take a parameter of type File_Type
  64. as their first parameter; if they're handed a File_Type, the subprogram
  65. will read or write to the given file.
  66. In general, if you don't want to use the default File_Type, add the
  67. File_Type as the first parameter of an input-output subprogram.
  68. <P>
  69. Here's a trivial example - a program that creates a new
  70. file called "hi" and writes text into it:
  71. <P>
  72. <TEXT FONT=PRE FILE="make_hi.adb">
  73. <P>
  74.  
  75. <QUESTION Type=Multiple-Choice>
  76. If you ran program Make_Hi twice, how many text
  77. lines would the file "hi" contain when you were done?
  78. <CHOICES>
  79. <CHOICE ANS=1>1.
  80. <CHOICE ANS=2>2.
  81. <CHOICE ANS=3>None.
  82. </CHOICES>
  83. <ANSWER ANS=1>
  84. <RESPONSES>
  85. <WHEN ANS=1>
  86. Right. Every time program Make_Hi runs, it creates a new file,
  87. erasing whatever was there before.
  88. <WHEN ANS=2>
  89. No, sorry.
  90. Re-read what the ``Create'' command does.
  91. </RESPONSES>
  92. <SECTION NAME="Line and File Endings">
  93. Package Text_IO models text files as a sequence of text lines;
  94. each text line has zero or more characters in it.
  95. Different operating systems have different ways to indicate
  96. the end of a line and the end of a file,
  97. so Text_IO detects them using the local operating system's conventions.
  98. <P>
  99. The following subprograms help deal with
  100. end of line and end of file:
  101. <DL>
  102. <DT>Procedure New_Line
  103. <DD>
  104. New_Line ends the current line and starts a new line.
  105. It takes an optional parameter indicating how many new lines to create
  106. (the default is one).
  107. You can also specify the file to output this new line to
  108. (the default is Current_Output).
  109. <DT>Procedure Skip_Line
  110. <DD>
  111. Skip_Line is the counterpart of New_Line;
  112. it gets ready to read the line after the current line,
  113. discarding any text on the current line that hasn't been read.
  114. <DT>Function End_Of_Line
  115. <DD>
  116. End_Of_Line detects whether or not the input is at the end of the line.
  117. <DT>Function End_Of_File
  118. <DD>
  119. End_Of_File detects whether or not the input is at the end of the file.
  120. <DT>Function Line
  121. <DD>
  122. Line reports the current line number of the file you're reading or writing
  123. (the first line is number 1).
  124. This is useful if you're processing some input data and you've suddenly
  125. found an input problem.
  126. </DL>
  127. <P>
  128. As with the Get and Put operations, put a File_Type as the first parameter
  129. if you want to work with a given file, or you'll use the default
  130. Current_Input and Current_Output.
  131. <P>
  132. These subprograms are quite useful without being passed any parameters.
  133. Note that in Ada, if you call a subprogram but don't want to pass it any
  134. parameters, don't include the parentheses() after the name of the subprogram
  135. (this is slightly different than C and C++).
  136. <P>
  137. Here's another demo program, one that only prints ``long'' lines.
  138. This demo program illustrates a very common Ada idiom - using
  139. ``while (not End_Of_File)'', which processes an entire input file.
  140. <P>
  141. <TEXT FONT=PRE FILE="put_long.adb">
  142. <P>
  143.  
  144.  
  145. <QUESTION Type=Multiple-Choice>
  146. If you want to discard the rest of
  147. a line of input, what subprogram would you use?
  148. <CHOICES>
  149. <CHOICE ANS=1>New_Line
  150. <CHOICE ANS=2>Skip_Line
  151. <CHOICE ANS=3>End_Of_Line
  152. </CHOICES>
  153. <ANSWER ANS=2>
  154. <RESPONSES>
  155. <WHEN ANS=1>
  156. No, sorry, New_Line <EM>outputs</EM> a new line.
  157. <WHEN ANS=2>
  158. Very nice.
  159. <WHEN ANS=3>
  160. No, End_Of_Line just reports if you've reached the end of a line or not.
  161. It doesn't change anything regarding the input.
  162. </RESPONSES>
  163. <SECTION NAME="Miscellaneous Text_IO Capabilities">
  164. Here are some miscellaneous capabilities of Text_IO that you may find useful.
  165. <P>
  166. Text_IO defines functions Current_Input, Current_Output, and Current_Error;
  167. each returns a File_Type.
  168. Current_Error is like Current_Output but should be used for
  169. error messages.
  170. Here's an example of printing an error message:
  171. <PRE>
  172.   Put(Current_Error, "Error - the widget file is missing.");
  173. </PRE>
  174. <P>
  175. Procedures Set_Input, Set_Output, and Set_Error let you set the
  176. current input, output, and error files somewhere else.
  177. For example, if you want all error messages to go to file "error", do
  178. the following:
  179. <PRE>
  180.   Error_File : File_Type;
  181.   -- ...
  182.   Create(Error_File, Out_File, "error");
  183.   Set_Error(Error_File);
  184. </PRE>
  185. <P>
  186. Ada and the underlying operating system
  187. may delay sending output to the user to increase overall performance.
  188. This is called buffering.
  189. Usually buffering is a good idea,
  190. but sometimes you want the output to be displayed right away.
  191. In that case, use the <EM>Flush</EM> operation to immediately display
  192. everything sent to a given file (the default is Current_Output).
  193. <P>
  194. Sometimes you want to check the keyboard to see if a user has pressed
  195. a key, and if so, what that key was.
  196. The Ada 95 procedure to do that is called <EM>Get_Immediate</EM>.
  197. There are a few caveats with Get_Immediate:
  198. <UL>
  199. <LI>
  200. On some systems it's not possible to implement Get_Immediate;
  201. in that case Ada can't do it (obviously).
  202. <LI>
  203. On multiprocessing systems it's often not a good idea to continuously
  204. check if something has happened.
  205. This is called polling, and polling can slow the whole system down.
  206. If you plan to just wait if a key hasn't been pressed, use <EM>Get</EM>
  207. instead.
  208. </UL>
  209. <SECTION NAME="Package Command_Line">
  210. There is a special kind of input and output between the
  211. operating system and the Ada program.
  212. This input is the information from the <EM>command line</EM>, and
  213. the output is what's called an <EM>exit status</EM>.
  214. This was easy to do in Ada 83 but there wasn't a standard way to do it.
  215. Ada 95 has added
  216. a standard package to perform this input and output called
  217. <EM>Command_Line</EM>.
  218. Command_Line is very similar to C's argc, argv, and exit.
  219. <P>
  220. Command_Line provides a few subprograms for input from the operating system.
  221. This package defines a function named <EM>Argument_Count</EM> that
  222. returns the number of arguments given to it (it will be zero or higher).
  223. It also provides a function called <EM>Argument</EM> that takes an
  224. argument number (an index) and returns that argument's value as a String.
  225. It also provides function Command_Name, which returns the name of the
  226. program as a String; some operating systems don't support this.
  227. <P>
  228. Command_Line provides subprograms for returning a <EM>Status_Code</EM>
  229. to the operating system, which is some sort of integer type.
  230. There are two predefined Status_Codes: Success and Failure.
  231. You can set the Status_Code using the procedure Set_Status; when the
  232. program ends, the last value set in Set_Status will be returned to the
  233. operating system.
  234. <P>
  235. Here's the definition of Command_Line
  236. (from the
  237. <A HREF="http://lglwww.epfl.ch/Ada/LRM/9X/rm9x/rm9x-A-15.html">LRM
  238. section A.15</A>):
  239. <P>
  240. <PRE>
  241.   package Ada.Command_Line is
  242.     function Argument_Count return Natural;
  243.     function Argument (Number : in Positive) return String;
  244.     function Command_Name return String;
  245.  
  246.     type Exit_Status is <EM>implementation-defined integer type;</EM>
  247.     Success : constant Exit_Status;
  248.     Failure : constant Exit_Status;
  249.     procedure Set_Exit_Status (Code : in Exit_Status);
  250.     -- ...
  251.   end Ada.Command_Line;
  252. </PRE>
  253. <P>
  254. Here's an example of Command_Line;
  255. program Show takes as arguments a list of files and prints them out,
  256. indented with one space:
  257. <P>
  258. <TEXT FONT=PRE FILE="show.adb">
  259. <P>
  260. Package Command_Line may not be useful if there's
  261. no operating system, since in that case there's nothing to communicate with.
  262.