home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Interactive Guide / c-cplusplus-interactive-guide.iso / c_ref / csource4 / 252_01 / chap09.txt < prev    next >
Text File  |  1990-02-15  |  29KB  |  648 lines

  1.  
  2.  
  3.  
  4.  
  5.                                                     Chapter 9
  6.                                         STANDARD INPUT/OUTPUT
  7.  
  8.  
  9. THE STDIO.H HEADER FILE
  10. ____________________________________________________________
  11.  
  12. Load the file SIMPLEIO.C for our first look    ==============
  13. at a file with standard I/O.  Standard I/O       SIMPLEIO.C
  14. refers to the most usual places where data is  ==============
  15. either read from, the keyboard, or written
  16. to, the video monitor.  Since they are used
  17. so much, they are used as the default I/O devices and do not
  18. need to be named in the Input/Output instructions.  This will
  19. make more sense when we actually start to use them so lets
  20. look at the file in front of you.
  21.  
  22. The first thing you should take notice of is the second line
  23. of the file, the line with #include "stdio.h".  This is very
  24. much like the #define we have already studied, except that
  25. instead of a simple substitution, an entire file is read in
  26. at this point.  The system will find the file named STDIO.H
  27. and read its entire contents in, replacing this statement. 
  28. Obviously then, the file named STDIO.H must contain valid C
  29. source statements that can be compiled as part of a program. 
  30. This particular file is composed of several standard #defines
  31. to define some of the standard I/O operations.  The file is
  32. called a header file and you will find several different
  33. header files on the source disks that came with your C
  34. compiler.  Each of the header files has a specific purpose and
  35. any or all of them can be included in any program.
  36.  
  37. Your C compiler uses the double quote marks to indicate that
  38. the search for the include file will begin in the current
  39. directory, and if it not found there, the search will continue
  40. in the include directory as set up in the environment.  It
  41. also uses the "less than" and "greater than" signs to indicate
  42. that the file search should begin in the directory specified
  43. in the environment.  Most of the programs in this tutorial
  44. have the double quotes in the include statements.  The next
  45. program uses the "<" and ">" to illustrate the usage.  Note
  46. that this will result is a slightly faster (but probably
  47. unnoticeable) compilation because the system will not bother
  48. to search the current directory first.
  49.  
  50.  
  51. INPUT/OUTPUT OPERATIONS IN C
  52. ____________________________________________________________
  53.  
  54. Actually the C programming language has no input or output
  55. operations defined as part of the language, they must be user
  56. defined.  Since everybody does not want to reinvent his own
  57. input and output operations, the compiler writers have done
  58.  
  59.                                                      Page 9-1
  60.  
  61.                             Chapter 9 - Standard Input/Output
  62.  
  63. a lot of this for us and supplied us with several input
  64. functions and several output functions to aid in our program
  65. development.  The functions have become a standard, and you
  66. will find the same functions available in nearly every
  67. compiler.  In fact, the industry standard of the C language
  68. definition has become the book written by Kernigan and
  69. Ritchie, and they have included these functions in their
  70. definition.  You will often, when reading literature about C,
  71. find an author refer to K & R.  This refers to the book, "The
  72. C Programming Language", written by Kernigan and Ritchie.  You
  73. would be advised to purchase a copy for reference.  As of this
  74. writing, a second edition of this book is available and is the
  75. preferred edition.
  76.  
  77. You should print out the file named STDIO.H and spend some
  78. time studying it.  There will be a lot that you will not
  79. understand about it, but parts of it will look familiar.  The
  80. name STDIO.H is sort of cryptic for "standard input/output
  81. header", because that is exactly what it is.  It defines the
  82. standard input and output functions in the form of #defines
  83. and macros.  Don't worry too much about the details of this
  84. now.  You can always return to this topic later for more study
  85. if it interests you, but you will really have no need to
  86. completely understand the STDIO.H file.  You will have a
  87. tremendous need to use it however, so these comments on its
  88. use and purpose are necessary.
  89.  
  90.  
  91. OTHER INCLUDE FILES
  92. ____________________________________________________________
  93.  
  94. When you begin writing larger programs and splitting them up
  95. into separately compiled portions, you will have occasion to
  96. use some statements common to each of the portions.  It would
  97. be to your advantage to make a separate file containing the
  98. statements and use the #include to insert it into each of the
  99. files.  If you want to change any of the common statements,
  100. you will only need to change one file and you will be assured
  101. of having all of the common statements agree.  This is getting
  102. a little ahead of ourselves but you now have an idea how the
  103. #include directive can be used.
  104.  
  105.  
  106. BACK TO THE FILE NAMED SIMPLEIO.C
  107. ____________________________________________________________
  108.  
  109. Lets continue our tour of the file in question.  The one
  110. variable named c is defined and a message is printed out with
  111. the familiar printf() function.  We then find ourselves in a
  112. continuous loop as long as the value of c is not equal to
  113. capital X.  If there is any question in your mind about the
  114. loop control, you should review chapter 3 before continuing. 
  115. The two new functions within the loop are of paramount
  116. interest in this program since they are the new functions.
  117.  
  118.                                                      Page 9-2
  119.  
  120.                             Chapter 9 - Standard Input/Output
  121.  
  122. These are functions to read a character from the keyboard and
  123. display it on the monitor one character at a time.
  124.  
  125. The function getchar() reads a single character from the
  126. standard input device, the keyboard being assumed because that
  127. is the standard input device, and assigns it to the variable
  128. named c.  The next function putchar(), uses the standard
  129. output device, the video monitor, and outputs the character
  130. contained in the variable named c.  The character is output
  131. at the current cursor location and the cursor is advanced one
  132. space for the next character.  The system is therefore taking
  133. care of a lot of the overhead for us.  The loop continues
  134. reading and displaying characters until we type a capital X
  135. which terminates the loop. 
  136.  
  137. Compile and run this program for a few surprises.  When you
  138. type on the keyboard, you will notice that what you type is
  139. displayed faithfully on the screen, and when you hit the
  140. return key, the entire line is repeated.  We only told it to
  141. output each character once but it seems to be saving the
  142. characters up and redisplaying them.  A short explanation is
  143. in order.
  144.  
  145.  
  146. DOS IS HELPING US OUT
  147. ____________________________________________________________
  148.  
  149. We need to understand a little bit about how DOS works to
  150. understand what is happening here.  When data is read from the
  151. keyboard, under DOS control, the characters are stored in a
  152. buffer until a carriage return is entered at which time the
  153. entire string of characters is given to the program.  When the
  154. characters are being typed, however, the characters are
  155. displayed one at a time on the monitor.  This is called echo,
  156. and happens in many of the applications you run. 
  157.  
  158. With the above paragraph in mind, it should be clear that when
  159. you are typing a line of data into SIMPLEIO, the characters
  160. are being echoed by DOS, and when you return the carriage by
  161. hitting return or enter, the characters are given to the
  162. program.  As each character is given to the program, it
  163. displays it on the screen resulting in a repeat of the line
  164. typed in.  To better illustrate this, type a line with a
  165. capital X somewhere in the middle of the line.  You can type
  166. as many characters as you like following the X and they will
  167. all display because the characters are being read in under
  168. DOS, echoed to the monitor, and placed in the DOS input
  169. buffer.  DOS doesn't think there is anything special about a
  170. capital X.  When the string is given to the program, however,
  171. the characters are accepted by the program one at a time and
  172. sent to the monitor one at a