home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / C / CTUTOR1.ZIP / CHAP9.TXT < prev    next >
Encoding:
Text File  |  1986-06-30  |  31.8 KB  |  719 lines

  1.                      Chapter 9 - Standard Input/Output
  2.  
  3.  
  4.                           THE STDIO.H HEADER FILE
  5.  
  6.              Load  the file SIMPLEIO.C for our first look at a  file 
  7.  
  8.         with  standard I/O.   Standard I/O refers to the most  usual 
  9.  
  10.         places  where  data is either read from,  the  keyboard,  or 
  11.  
  12.         written to, the video monitor.  Since they are used so much, 
  13.  
  14.         they are used as the default I/O devices and do not need  to 
  15.  
  16.         be  named in the Input/Output instructions.   This will make 
  17.  
  18.         more  sense when we actually start to use them so lets  look 
  19.  
  20.         at the file in front of you.
  21.  
  22.              The  first thing you will notice is the first  line  of 
  23.  
  24.         the  file,  the #include "stdio.h" line.   This is very much 
  25.  
  26.         like  the  #define  we have  already  studied,  except  that 
  27.  
  28.         instead of a simple substitution,  an entire file is read in 
  29.  
  30.         at  this  point.   The  system  will  find  the  file  named 
  31.  
  32.         "stdio.h"  and read its entire contents in,  replacing  this 
  33.  
  34.         statement.   Obviously then,  the file named "stdio.h"  must 
  35.  
  36.         contain  valid  C source statements that can be compiled  as 
  37.  
  38.         part  of  a program.   This particular file is  composed  of 
  39.  
  40.         several standard #defines to define some of the standard I/O 
  41.  
  42.         operations.   The file is called a header file and you  will 
  43.  
  44.         find several different header files on the source disks that 
  45.  
  46.         came  with  your compiler.   Each of the header files has  a 
  47.  
  48.         specific  purpose and any or all of them can be included  in 
  49.  
  50.         any program.
  51.  
  52.              Most C compilers use the double quote marks to indicate 
  53.  
  54.         that  the  "include"  file  will be  found  in  the  current 
  55.  
  56.         directory.   A  few use the "less than" and  "greater  than" 
  57.  
  58.         signs  to indicate that the file will be found in a standard 
  59.  
  60.         header  file.   Nearly all MSDOS C compilers use the  double 
  61.  
  62.         quotes,  and  most require the "include" file to be  in  the 
  63.  
  64.         default  directory.   All  of the programs in this  tutorial 
  65.  
  66.         have the double quotes in the "include" statements.  If your 
  67.  
  68.         compiler  uses the other notation,  you will have to  change 
  69.  
  70.         them before compiling.
  71.  
  72.                         INPUT/OUTPUT OPERATIONS IN C
  73.  
  74.              Actually  the  C programming language has no  input  or 
  75.  
  76.         output operations defined as part of the language, they must 
  77.  
  78.         be user defined.   Since everybody does not want to reinvent 
  79.  
  80.         his  own input and output operations,  the compiler  writers 
  81.  
  82.         have done a lot of this for us and supplied us with  several 
  83.  
  84.         input  functions and several output functions to aid in  our 
  85.  
  86.         program development.   The functions have become a standard, 
  87.  
  88.         and  you  will find the same functions available  in  nearly 
  89.  
  90.         every  compiler.   In fact,  the industry standard of the  C 
  91.  
  92.         language  definition has become the book written by Kernigan 
  93.  
  94.         and Ritchie, and they have included these functions in their 
  95.  
  96.  
  97.  
  98.                                   Page 55
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.                      Chapter 9 - Standard Input/Output
  109.  
  110.  
  111.         definition.   You will often,  when reading literature about 
  112.  
  113.         C,  find  a  reference to K & R.   This refers to  the  book 
  114.  
  115.         written  by Kernigan and Ritchie.   You would be advised  to 
  116.  
  117.         purchase a copy for reference.
  118.  
  119.              You should print out the file named "stdio.h" and spend 
  120.  
  121.         some  time studying it.   There will be a lot that you  will 
  122.  
  123.         not understand about it, but parts of it will look familiar.  
  124.  
  125.         The  name  "stdio.h"  is  sort  of  cryptic  for   "standard 
  126.  
  127.         input/output header",  because that is exactly what it does.  
  128.  
  129.         It  defines  the standard input and output functions in  the 
  130.  
  131.         form of #defines and macros.  Don't worry too much about the 
  132.  
  133.         details  of this now.   You can always return to this  topic 
  134.  
  135.         later  for  more study if it interests  you,  but  you  will 
  136.  
  137.         really  have no need to completely understand the  "stdio.h" 
  138.  
  139.         file.  You will have a tremendous need to use it however, so 
  140.  
  141.         these comments on its use and purpose are necessary.
  142.  
  143.                             OTHER INCLUDE FILES
  144.  
  145.              When  you  begin writing larger programs and  splitting 
  146.  
  147.         them  up into separately compiled portions,  you  will  have 
  148.  
  149.         occasion  to  use  some  statements common to  each  of  the 
  150.  
  151.         portions.   It would be to your advantage to make a separate 
  152.  
  153.         file  containing  the  statements and use  the  #include  to 
  154.  
  155.         insert it into each of the files.  If you want to change any 
  156.  
  157.         of the common statements,  you will only need to change  one 
  158.  
  159.         file  and  you will be assured of having all of  the  common 
  160.  
  161.         statements  agree.   This  is  getting  a  little  ahead  of 
  162.  
  163.         ourselves  but  you  now  have  an  idea  how  the  #include 
  164.  
  165.         directive can be used.
  166.  
  167.                     BACK TO THE FILE NAMED "SIMPLEIO.C"
  168.              
  169.              Lets  continue our tour of the file in  question.   The 
  170.  
  171.         one  variable  "c" is defined and a message is  printed  out 
  172.  
  173.         with the familiar "printf" function.  We then find ourselves 
  174.  
  175.         in  a continuous loop as long as "c" is not equal to capital 
  176.  
  177.         X.   If  there is any question in your mind about  the  loop 
  178.  
  179.         control, you should review chapter 3 before continuing.  The 
  180.  
  181.         two  new functions within the loop are of paramount interest 
  182.  
  183.         in this program since they are the new functions.  These are 
  184.  
  185.         functions to read a character from the keyboard and  display 
  186.  
  187.         it on the monitor one character at a time.
  188.  
  189.              The  function "getchar()" reads a single character from 
  190.  
  191.         the  standard  input  device,  the  keyboard  being  assumed 
  192.  
  193.         because that is the standard input device, and assigns it to 
  194.  
  195.         the variable "c".   The next function "putchar(c)", uses the 
  196.  
  197.         standard output device,  the video monitor,  and outputs the 
  198.  
  199.         character contained in the variable "c".   The character  is 
  200.  
  201.  
  202.  
  203.                                   Page 56
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211.  
  212.  
  213.                      Chapter 9 - Standard Input/Output
  214.  
  215.  
  216.         output  at  the  current cursor location and the  cursor  is 
  217.  
  218.         advanced  one space for the next character.   The system  is 
  219.  
  220.         therefore taking care of a lot of the overhead for us.   The 
  221.  
  222.         loop  continues reading and displaying characters  until  we 
  223.  
  224.         type a capital X which terminates the loop. 
  225.  
  226.              Compile and run this program for a few surprises.  When 
  227.  
  228.         you type on the keyboard, you will notice that what you type 
  229.  
  230.         is displayed faithfully on the screen,  and when you hit the 
  231.  
  232.         return key,  the entire line is repeated.   In fact, we only 
  233.  
  234.         told  it  to output each character once but it seems  to  be 
  235.  
  236.         saving  the  characters up and redisplaying them.   A  short 
  237.  
  238.         explanation is in order.
  239.  
  240.                DOS IS HELPING US OUT (OR GETTING IN THE WAY)
  241.  
  242.              We need to understand a little bit about how DOS  works 
  243.  
  244.         to  understand  what is happening here.   When data is  read 
  245.  
  246.         from  the keyboard,  under DOS control,  the characters  are 
  247.  
  248.         stored  in  a buffer until a carriage return is  entered  at 
  249.  
  250.         which  time the entire string of characters is given to  the 
  251.  
  252.         program.   When the characters are being typed, however, the 
  253.  
  254.         characters are displayed one at a time on the monitor.  This 
  255.  
  256.         is called echo,  and happens in many of the applications you 
  257.  
  258.         run. 
  259.  
  260.              With  the above paragraph in mind,  it should be  clear 
  261.  
  262.         that when you are typing a line of data into "SIMPLEIO", the 
  263.  
  264.         characters are being echoed by DOS,  and when you return the 
  265.  
  266.         carriage,  the characters are given to the program.  As each 
  267.  
  268.         character  is given to the program,  it displays it  on  the 
  269.  
  270.         screen  resulting  in  a repeat of the line  typed  in.   To 
  271.  
  272.         better  illustrate  this,  type  a line  with  a  capital  X 
  273.  
  274.         somewhere  in the middle of the line.   You can type as many 
  275.  
  276.         characters  as you like following the "X" and they will  all 
  277.  
  278.         display because the characters are being read in under  DOS, 
  279.  
  280.         echoed  to the monitor,  and placed in the DOS input buffer.  
  281.  
  282.         DOS doesn't think there is anything special about a  capital 
  283.  
  284.         X.   When the string is given to the program,  however,  the 
  285.  
  286.         characters  are  accepted by the program one at a  time  and 
  287.  
  288.         sent  to  the monitor one at a time,  until a capital  X  is 
  289.  
  290.         encountered.   After the capital X is displayed, the loop is 
  291.  
  292.         terminated,  and the program is terminated.   The characters 
  293.  
  294.         on  the input line following the capital X are not displayed 
  295.  
  296.         because the capital X signalled program termination.
  297.  
  298.              Compile  and  run  "SIMPLEIO.C".    After  running  the 
  299.  
  300.         program  several  times  and  feeling  confidant  that   you 
  301.  
  302.         understand  the above explanation,  we will go on to another 
  303.  
  304.         program.
  305.  
  306.  
  307.  
  308.  
  309.                                   Page 57
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.                      Chapter 9 - Standard Input/Output
  320.  
  321.  
  322.              Don't  get  discouraged by the  above  seemingly  weird 
  323.  
  324.         behavior  of the I/O system.   It is strange,  but there are 
  325.  
  326.         other ways to get data into the computer.  You will actually 
  327.  
  328.         find the above method useful for many applications,  and you 
  329.  
  330.         will probably find some of the following useful also.
  331.  
  332.                          ANOTHER STRANGE I/O METHOD
  333.  
  334.              Load  the file named SINGLEIO.C and display it on  your 
  335.  
  336.         monitor for another method of character I/O.  Once again, we 
  337.  
  338.         start  with  the  standard I/O  header  file,  we  define  a 
  339.  
  340.         variable named "c",  and we print a welcoming message.  Like 
  341.  
  342.         the  last  program,  we are in a loop that will continue  to 
  343.  
  344.         execute  until  we type a capital X,  but the  action  is  a 
  345.  
  346.         little different here.
  347.  
  348.              The  "getch()"  is  a  new  function  that  is  a  "get 
  349.  
  350.         character" function.  It differs from "getchar()" in that it 
  351.  
  352.         does  not  get tied up in DOS.   It reads the  character  in 
  353.  
  354.         without echo, and puts it directly into the program where it 
  355.  
  356.         is  operated  on immediately.   This function then  reads  a 
  357.  
  358.         character,  immediately  displays  it  on  the  screen,  and 
  359.  
  360.         continues the operation until a capital X is typed.
  361.  
  362.              When  you compile and run this program,  you will  find 
  363.  
  364.         that there is no repeat of the lines when you hit a carriage 
  365.  
  366.         return,  and  when  you  hit  the  capital  X,  the  program 
  367.  
  368.         terminates immediately.  No carriage return is needed to get 
  369.  
  370.         it to accept the line with the X in it.   We do have another 
  371.  
  372.         problem here, there is no linefeed with the carriage return.
  373.  
  374.                           NOW WE NEED A LINE FEED
  375.  
  376.              It  is not apparent to you in most application programs 
  377.  
  378.         but  when  you hit the enter key,  the  program  supplies  a 
  379.  
  380.         linefeed to go with the carriage return.  You need to return 
  381.  
  382.         to  the  left side of the monitor and you also need to  drop 
  383.  
  384.         down  a line.   The linefeed is not automatic.  We  need  to 
  385.  
  386.         improve  our program to do this also.   If you will load and 
  387.  
  388.         display the program named BETTERIN.C, you will find a change 
  389.  
  390.         to incorporate this feature.
  391.  
  392.              In BETTERIN.C, we have two additional statements at the 
  393.  
  394.         beginning  that  will  define the character  codes  for  the 
  395.  
  396.         linefeed (LF), and the carriage return (CR).  If you look at 
  397.  
  398.         any  ASCII table you will find that the codes 10 and 13  are 
  399.  
  400.         exactly  as  defined  here.   In  the  main  program,  after 
  401.  
  402.         outputting the character,  we compare it to CR, and if it is 
  403.  
  404.         equal to CR,  we also output a linefeed which is the LF.  We 
  405.  
  406.         could  have  just  as well have left  out  the  two  #define 
  407.  
  408.         statements and used "if (c == 13) putchar(10);" but it would 
  409.  
  410.  
  411.  
  412.                                   Page 58
  413.  
  414.  
  415.  
  416.  
  417.  
  418.  
  419.  
  420.  
  421.  
  422.                      Chapter 9 - Standard Input/Output
  423.  
  424.  
  425.         not  be  very descriptive of what we are  doing  here.   The 
  426.  
  427.         method  used  in the program represents  better  programming 
  428.  
  429.         practice.
  430.  
  431.              Compile  and  run BETTERIN.C to see if it does what  we 
  432.  
  433.         have said it should do.   It should display exactly what you 
  434.  
  435.         type in, including a linefeed with each carriage return, and 
  436.  
  437.         should stop immediately when you type a capital X. 
  438.  
  439.              If  you are using a nonstandard compiler,  it  may  not 
  440.  
  441.         find  a "CR" because your system returns a "LF" character to 
  442.  
  443.         indicate  end-of-line.   It will be up to you  to  determine 
  444.  
  445.         what method your compiler uses.   The quickest way is to add 
  446.  
  447.         a  "printf"  statement  that prints the input  character  in 
  448.  
  449.         decimal format.
  450.  
  451.                            WHICH METHOD IS BEST?
  452.  
  453.              We have examined two methods of reading characters into 
  454.  
  455.         a  C program,  and are faced with a choice of which  one  we 
  456.  
  457.         should  use.   It really depends on the application  because 
  458.  
  459.         each  method has advantages and disadvantages.   Lets take a 
  460.  
  461.         look at each. 
  462.  
  463.              When using the first method,  DOS is actually doing all 
  464.  
  465.         of  the  work for us by storing the characters in  an  input 
  466.  
  467.         buffer and signalling us when a full line has been  entered.  
  468.  
  469.         We  could write a program that,  for example,  did a lot  of 
  470.  
  471.         calculations,  then  went to get some input.   While we were 
  472.  
  473.         doing the calculations,  DOS would be accumulating a line of 
  474.  
  475.         characters  for  us,  and they would be there when  we  were 
  476.  
  477.         ready  for  them.   However,  we could not  read  in  single 
  478.  
  479.         keystrokes   because  DOS  would  not  report  a  buffer  of 
  480.  
  481.         characters to us until it recognized a carriage return.
  482.  
  483.              The second method, used in BETTERIN.C, allows us to get 
  484.  
  485.         a single character,  and act on it immediately.   We do  not 
  486.  
  487.         have  to  wait  until  DOS decides we can  have  a  line  of 
  488.  
  489.         characters.  We cannot do anything else while we are waiting 
  490.  
  491.         for  a  character  because  we are  waiting  for  the  input 
  492.  
  493.         keystroke  and tying up the entire machine.   This method is 
  494.  
  495.         useful  for highly interactive types of program  interfaces.  
  496.  
  497.         It  is up to you as the programmer to decide which  is  best 
  498.  
  499.         for your needs.
  500.  
  501.              I  should  mention at this point that there is also  an 
  502.  
  503.         "ungetch" function that works with the "getch" function.  If 
  504.  
  505.         you "getch" a character and find that you have gone one  too 
  506.  
  507.         far,  you  can "ungetch" it back to the input device.   This 
  508.  
  509.         simplifies  some  programs because you don't know  that  you 
  510.  
  511.         don't  want the character until you get it.   You  can  only 
  512.  
  513.  
  514.  
  515.                                   Page 59
  516.  
  517.  
  518.  
  519.  
  520.  
  521.  
  522.  
  523.  
  524.  
  525.                      Chapter 9 - Standard Input/Output
  526.  
  527.  
  528.         "ungetch"  one character back to the input device,  but that 
  529.  
  530.         is  sufficient  to  accomplish the task  this  function  was 
  531.  
  532.         designed for.   It is difficult to demonstrate this function 
  533.  
  534.         in  a simple program so its use will be up to you  to  study 
  535.  
  536.         when you need it.
  537.  
  538.              The discussion so far in this chapter, should be a good 
  539.  
  540.         indication  that,  while the C programming language is  very 
  541.  
  542.         flexible,  it does put a lot of responsibility on you as the 
  543.  
  544.         programmer to keep many details in mind.
  545.  
  546.                         NOW TO READ IN SOME INTEGERS
  547.  
  548.              Load  and display the file named INTIN.C for an example 
  549.  
  550.         of  reading in some formatted data.   The structure of  this 
  551.  
  552.         program  is  very similar to the last three except  that  we 
  553.  
  554.         define  an "int" type variable and loop until  the  variable 
  555.  
  556.         somehow acquires the value of 100.
  557.  
  558.              Instead of reading in a character at a time, as we have 
  559.  
  560.         in the last three files,  we read in an entire integer value 
  561.  
  562.         with  one  call  using the  function  named  "scanf".   This 
  563.  
  564.         function  is very similar to the "printf" that you have been 
  565.  
  566.         using for quite some time by now except that it is used  for 
  567.  
  568.         input instead of output.   Examine the line with the "scanf" 
  569.  
  570.         and  you  will notice that it does not ask for the  variable 
  571.  
  572.         "valin"  directly,  but  gives the address of  the  variable 
  573.  
  574.         since it expects to have a value returned from the function.  
  575.  
  576.         Recall  that a function must have the address of a  variable 
  577.  
  578.         in  order  to  return  the value  to  the  calling  program.  
  579.  
  580.         Failing  to  supply  a pointer in the  "scanf"  function  is 
  581.  
  582.         probably  the most common problem encountered in using  this 
  583.  
  584.         function. 
  585.  
  586.              The  function  "scanf" scans the input  line  until  it 
  587.  
  588.         finds  the first data field.   It ignores leading blanks and 
  589.  
  590.         in this case,  it reads integer characters until it finds  a 
  591.  
  592.         blank  or  an invalid decimal character,  at which  time  it 
  593.  
  594.         stops reading and returns the value. 
  595.  
  596.              Remembering  our discussion above about the way the DOS 
  597.  
  598.         input  buffer  works,  it should be clear  that  nothing  is 
  599.  
  600.         actually acted on until a complete line is entered and it is 
  601.  
  602.         terminated by a carriage return.   At this time,  the buffer 
  603.  
  604.         is  input,  and  our  program will search  across  the  line 
  605.  
  606.         reading  all  integer values it can find until the  line  is 
  607.  
  608.         completely scanned.  This is because we are in a loop and we 
  609.  
  610.         tell it to find a value,  print it,  find another, print it, 
  611.  
  612.         etc.   If you enter several values on one line, it will read 
  613.  
  614.         each one in succession and display the values.  Entering the 
  615.  
  616.         value  of  100  will cause the  program  to  terminate,  and 
  617.  
  618.  
  619.  
  620.                                   Page 60
  621.  
  622.  
  623.  
  624.  
  625.  
  626.  
  627.  
  628.  
  629.  
  630.                      Chapter 9 - Standard Input/Output
  631.  
  632.  
  633.         entering  the  value 100 with other values  following,  will 
  634.  
  635.         cause   termination   before  the   following   values   are 
  636.  
  637.         considered. 
  638.  
  639.                       IT MAKES WRONG ANSWERS SOMETIMES
  640.  
  641.              If  you  enter a number up to and including  32767,  it 
  642.  
  643.         will display correctly, but if you enter a larger number, it 
  644.  
  645.         will appear to make an error.  For example, if you enter the 
  646.  
  647.         value 32768,  it will display the value of -32768,  entering 
  648.  
  649.         the  value  65536 will display as a  zero.   These  are  not 
  650.  
  651.         errors but are caused by the way an integer is defined.  The 
  652.  
  653.         most significant bit of the 16 bit pattern available for the 
  654.  
  655.         integer variable is the sign bit,  so there are only 15 bits 
  656.  
  657.         left  for the value.   The variable can therefore only  have 
  658.  
  659.         the  values  from  -32768 to 32767,  any  other  values  are 
  660.  
  661.         outside  the range of integer variables.   This is up to you 
  662.  
  663.         to take care of in your programs.   It is another example of 
  664.  
  665.         the increased responsibility you must assume using C  rather 
  666.  
  667.         than a higher level language such as Pascal, Modula-2, etc.
  668.  
  669.              The   above  paragraph  is  true  for  most  MS-DOS   C 
  670.  
  671.         compilers.   There  is  a very small possibility  that  your 
  672.  
  673.         compiler uses an integer value other than 16 bits.   If that 
  674.  
  675.         is  the  case,  the  same  principles will be  true  but  at 
  676.  
  677.         different limits than those given above.
  678.  
  679.              Compile and run this program,  entering several numbers 
  680.  
  681.         on  a line to see the results,  and with varying numbers  of 
  682.  
  683.         blanks between the numbers.   Try entering numbers that  are 
  684.  
  685.         too big to see what happens,  and finally enter some invalid 
  686.  
  687.         characters  to  see  what the system  does  with  nondecimal 
  688.  
  689.         characters.
  690.  
  691.                            CHARACTER STRING INPUT
  692.  
  693.              Load  and  display  the file named  STRINGIN.C  for  an 
  694.  
  695.         example  of  reading  a string variable.   This  program  is 
  696.  
  697.         identical to the last one except that instead of an  integer 
  698.  
  699.         variable,  we  have defined a string variable with an  upper 
  700.  
  701.         limit of 24 characters (remember that a string variable must 
  702.  
  703.         have  a  null character at the end).   The variable  in  the 
  704.  
  705.         "scanf"  does  not  need  an & because  "big"  is  an  array 
  706.  
  707.         variable  and by definition it is already a  pointer.   This 
  708.  
  709.         program  should require no additional explanation.   Compile 
  710.  
  711.         and run it to see if it works the way you expect.
  712.  
  713.              You probably got a surprise when you ran it because  it 
  714.  
  715.         separated  your sentence into separate words.   When used in 
  716.  
  717.         the string mode of input,  "scanf" reads characters into the 
  718.  
  719.         string until it comes to either the end of a line or a blank 
  720.  
  721.  
  722.  
  723.                                   Page 61
  724.  
  725.  
  726.  
  727.  
  728.  
  729.  
  730.  
  731.  
  732.  
  733.                      Chapter 9 - Standard Input/Output
  734.  
  735.  
  736.         character.   Therefore,  it  reads a word,  finds the  blank 
  737.  
  738.         following it,  and displays the result.   Since we are in  a 
  739.  
  740.         loop, this program continues to read words until it exhausts 
  741.  
  742.         the DOS input buffer.   We have written this program to stop 
  743.  
  744.         whenever  it  finds a capital X in column 1,  but since  the 
  745.  
  746.         sentence  is split up into individual words,  it  will  stop 
  747.  
  748.         anytime a word begins with capital X.  Try entering a 5 word 
  749.  
  750.         sentence  with  a  capital X as the first character  in  the 
  751.  
  752.         third word.  You should get the first three words displayed, 
  753.  
  754.         and the last two simply ignored when the program stops.
  755.  
  756.              Try  entering more than 24 characters to see  what  the 
  757.  
  758.         program does.  It should generate an error, but that will be 
  759.  
  760.         highly dependent on the system you are using.   In an actual 
  761.  
  762.         program,  it  is your responsibility to count characters and 
  763.  
  764.         stop when the input buffer is full.   You may be getting the 
  765.  
  766.         feeling  that a lot of responsibility is placed on you  when 
  767.  
  768.         writing in C.   It is, but you also get a lot of flexibility 
  769.  
  770.         in the bargain too.
  771.  
  772.                        INPUT/OUTPUT PROGRAMMING IN C
  773.  
  774.              C was not designed to be used as a language for lots of 
  775.  
  776.         input and output,  but as a systems language where a lot  of 
  777.  
  778.         internal operations are required.   You would do well to use 
  779.  
  780.         another language for I/O intensive programming,  but C could 
  781.  
  782.         be used if you desire.  The keyboard input is very flexible, 
  783.  
  784.         allowing you to get at the data in a very low level way, but 
  785.  
  786.         very little help is given you.  It is therefore up to you to 
  787.  
  788.         take  care of all of the bookkeeping chores associated  with 
  789.  
  790.         your  required  I/O operations.   This may seem like a  real 
  791.  
  792.         pain in the neck, but in any given program, you only need to 
  793.  
  794.         define your input routines once and then use them as needed.
  795.  
  796.              Don't let this worry you.   As you gain experience with 
  797.  
  798.         C, you will easily handle your I/O requirements.
  799.  
  800.              One final point must be made about these I/O functions.  
  801.  
  802.         It   is  perfectly  permissible  to  intermix  "scanf"   and 
  803.  
  804.         "getchar"  functions during read operations.   In  the  same 
  805.  
  806.         manner,  it  is also fine to intermix the output  functions, 
  807.  
  808.         "printf" and "putchar". 
  809.  
  810.                                IN MEMORY I/O
  811.  
  812.              The  next operation may seem a little strange at first, 
  813.  
  814.         but  you will probably see lots of uses for it as  you  gain 
  815.  
  816.         experience.   Load the file named INMEM.C and display it for 
  817.  
  818.         another  type  of I/O,  one that never accesses the  outside 
  819.  
  820.         world, but stays in the computer.
  821.  
  822.  
  823.  
  824.  
  825.                                   Page 62
  826.  
  827.  
  828.  
  829.  
  830.  
  831.  
  832.  
  833.  
  834.  
  835.                      Chapter 9 - Standard Input/Output
  836.  
  837.  
  838.              In INMEM.C, we define a few variables, then assign some 
  839.  
  840.         values to the ones named "numbers" for illustrative purposes 
  841.  
  842.         and then use a "sprintf" function.   The function acts  just 
  843.  
  844.         like  a  normal  "printf" function except  that  instead  of 
  845.  
  846.         printing the line of output to a device,  it prints the line 
  847.  
  848.         of  formatted  output to a character string in  memory.   In 
  849.  
  850.         this  case  the string goes to the string  variable  "line", 
  851.  
  852.         because  that  is the string name we inserted as  the  first 
  853.  
  854.         argument  in the "sprintf" function.   The spaces after  the 
  855.  
  856.         2nd  %d were put there to illustrate that the next  function 
  857.  
  858.         will  search  properly  across  the  line.    We  print  the 
  859.  
  860.         resulting  string and find that the output is  identical  to 
  861.  
  862.         what  it would have been by using a "printf" instead of  the 
  863.  
  864.         "sprintf"  in the first place.   You will see that when  you 
  865.  
  866.         compile and run the program shortly.
  867.  
  868.              Since  the generated string is still in memory,  we can 
  869.  
  870.         now  read  it  with the  function  "sscanf".   We  tell  the 
  871.  
  872.         function  in its first argument that "line" is the string to 
  873.  
  874.         use for its input,  and the remaining parts of the line  are 
  875.  
  876.         exactly  what  we  would  use if we were going  to  use  the 
  877.  
  878.         "scanf"  function and read data from outside  the  computer.  
  879.  
  880.         Note  that it is essential that we use pointers to the  data 
  881.  
  882.         because  we  want to return data from a function.   Just  to 
  883.  
  884.         illustrate  that  there are many ways to declare  a  pointer 
  885.  
  886.         several methods are used,  but all are pointers.   The first 
  887.  
  888.         two simply declare the address of the elements of the array, 
  889.  
  890.         while the last three use the fact that "result", without the 
  891.  
  892.         accompanying  subscript,  is  a pointer.   Just to  keep  it 
  893.  
  894.         interesting,  the  values  are read back in  reverse  order.  
  895.  
  896.         Finally the values are displayed on the monitor. 
  897.  
  898.                            IS THAT REALLY USEFUL?
  899.  
  900.              It  seems sort of silly to read input data from  within 
  901.  
  902.         the  computer  but  it does have  a  real  purpose.   It  is 
  903.  
  904.         possible to read data in using any of the standard functions 
  905.  
  906.         and  then do a format conversion in memory.   You could read 
  907.  
  908.         in  a line of data,  look at a few  significant  characters, 
  909.  
  910.         then  use these formatted input routines to reduce the  line 
  911.  
  912.         of  data to internal representation.   That would sure  beat 
  913.  
  914.         writing your own data formatting routines.
  915.  
  916.                            STANDARD ERROR OUTPUT
  917.  
  918.              Sometimes  it is desirable to redirect the output  from 
  919.  
  920.         the  standard  output device to a file.   However,  you  may 
  921.  
  922.         still  want the error messages to go to the standard  output 
  923.  
  924.         device,  in our case the monitor.  This next function allows 
  925.  
  926.         you to do that. Load and display SPECIAL.C for an example of 
  927.  
  928.         this new function.
  929.  
  930.  
  931.  
  932.                                   Page 63
  933.  
  934.  
  935.  
  936.  
  937.  
  938.  
  939.  
  940.  
  941.  
  942.                      Chapter 9 - Standard Input/Output
  943.  
  944.  
  945.  
  946.              The  program  consists  of a  loop  with  two  messages 
  947.  
  948.         output,  one  to the standard output device and the other to 
  949.  
  950.         the  standard  error device.   The message to  the  standard 
  951.  
  952.         error  device  is  output with the  function  "fprintf"  and 
  953.  
  954.         includes  the  device name "stderr" as the  first  argument.  
  955.  
  956.         Other  than those two small changes,  it is the same as  our 
  957.  
  958.         standard  "printf"  function.   (You will see  more  of  the 
  959.  
  960.         "fprintf"  function in the next chapter,  but its  operation 
  961.  
  962.         fit  in better as a part of this chapter.)  Ignore the  line 
  963.  
  964.         with the "exit" for the moment, we will return to it.
  965.  
  966.              Compile  and  run this program,  and you will  find  12 
  967.  
  968.         lines of output on the monitor.   To see the difference, run 
  969.  
  970.         the  program  again with redirected output to a  file  named 
  971.  
  972.         "STUFF" by entering the following line at the Dos prompt;
  973.  
  974.         A> special >stuff
  975.  
  976.              More  information about I/O redirection can be found in 
  977.  
  978.         your  DOS manual.   This time you will only get the 6  lines 
  979.  
  980.         output to the standard error device, and if you look in your 
  981.  
  982.         directory,  you will find the file named "STUFF"  containing 
  983.  
  984.         the other 6 lines, those to the standard output device.  You 
  985.  
  986.         can use I/O redirection with any of the programs we have run 
  987.  
  988.         so far,  and as you may guess, you can also read from a file 
  989.  
  990.         using I/O redirection but we will study a better way to read 
  991.  
  992.         from a file in the next chapter.
  993.  
  994.                      WHAT ABOUT THE exit(4) STATEMENT?
  995.  
  996.              Now  to  keep our promise about the exit(4)  statement. 
  997.  
  998.         Redisplay  the file named SPECIAL.C on  your  monitor.   The 
  999.  
  1000.         last  statement  simply  exits the program and  returns  the 
  1001.  
  1002.         value  of 4 to DOS.   Any number from 0 to 9 can be used  in 
  1003.  
  1004.         the parentheses for DOS communication.  If you are operating 
  1005.  
  1006.         in  a  BATCH  file,  this  number can  be  tested  with  the 
  1007.  
  1008.         "ERRORLEVEL" command.
  1009.  
  1010.              Most compilers that operate in several passes return  a 
  1011.  
  1012.         1  with  this mechanism to indicate that a fatal  error  has 
  1013.  
  1014.         occurred and it would be a waste of time to go on to another 
  1015.  
  1016.         pass resulting in even more errors.
  1017.  
  1018.              It  is therefore wise to use a batch file for compiling 
  1019.  
  1020.         programs and testing the returned value for errors.  A check 
  1021.  
  1022.         of  the documentation for my COMPAQ,  resulted in a  minimal 
  1023.  
  1024.         and confusing documentation of the "errorlevel" command,  so 
  1025.  
  1026.         a brief description of it is given in this file.
  1027.  
  1028.  
  1029.  
  1030.  
  1031.  
  1032.                                   Page 64
  1033.  
  1034.  
  1035.  
  1036.  
  1037.  
  1038.  
  1039.  
  1040.  
  1041.  
  1042.                      Chapter 9 - Standard Input/Output
  1043.  
  1044.  
  1045.         PROGRAMMING EXERCISE
  1046.  
  1047.         1.   Write  a program to  read in a character using a  loop, 
  1048.  
  1049.              and  display the character in its normal  "char"  form.      
  1050.  
  1051.              Also  display  it  as a decimal  number.  Check  for  a      
  1052.  
  1053.              dollar  sign  to use as the  stop  character.  Use  the      
  1054.  
  1055.              "getch" form of input so it will print immediately. Hit 
  1056.  
  1057.              some of the special keys,  such as function keys,  when 
  1058.  
  1059.              you  run the program for some surprises.  You will  get 
  1060.  
  1061.              two  inputs  from the special keys,  the first being  a 
  1062.  
  1063.              zero  which  is  the indication to the  system  that  a 
  1064.  
  1065.              special key was hit.
  1066.  
  1067.  
  1068.  
  1069.  
  1070.  
  1071.  
  1072.  
  1073.  
  1074.  
  1075.  
  1076.  
  1077.  
  1078.  
  1079.  
  1080.  
  1081.  
  1082.  
  1083.  
  1084.  
  1085.  
  1086.  
  1087.  
  1088.  
  1089.  
  1090.  
  1091.  
  1092.  
  1093.  
  1094.  
  1095.  
  1096.  
  1097.  
  1098.  
  1099.  
  1100.  
  1101.  
  1102.  
  1103.  
  1104.  
  1105.  
  1106.  
  1107.                                   Page 65
  1108.  
  1109.