home *** CD-ROM | disk | FTP | other *** search
/ Collection of Hack-Phreak Scene Programs / cleanhpvac.zip / cleanhpvac / GENCTXT.ZIP / CHAP9.TXT < prev    next >
Text File  |  1987-11-21  |  32KB  |  719 lines

  1.  
  2.                      Chapter 9 - Standard Input/Output
  3.  
  4.  
  5.                           THE STDIO.H HEADER FILE
  6.  
  7.              Load  the file SIMPLEIO.C for our first look at a  file
  8.         with  standard I/O.   Standard I/O refers to the most  usual
  9.         places  where  data is either read from,  the  keyboard,  or
  10.         written to, the video monitor.  Since they are used so much,
  11.         they are used as the default I/O devices and do not need  to
  12.         be  named in the Input/Output instructions.   This will make
  13.         more  sense when we actually start to use them so lets  look
  14.         at the file in front of you.
  15.  
  16.              The  first thing you will notice is the second line  of
  17.         the  file,  the #include "stdio.h" line.   This is very much
  18.         like  the  #define  we have  already  studied,  except  that
  19.         instead of a simple substitution,  an entire file is read in
  20.         at  this  point.   The  system  will  find  the  file  named
  21.         "stdio.h"  and read its entire contents in,  replacing  this
  22.         statement.   Obviously then,  the file named "stdio.h"  must
  23.         contain  valid  C source statements that can be compiled  as
  24.         part  of  a program.   This particular file is  composed  of
  25.         several standard #defines to define some of the standard I/O
  26.         operations.   The file is called a header file and you  will
  27.         find several different header files on the source disks that
  28.         came  with your C compiler.  Each of the header files has  a
  29.         specific  purpose and any or all of them can be included  in
  30.         any program.
  31.  
  32.              Your C compiler uses the double quote marks to indicate
  33.         that  the  search for the "include" file will begin  in  the
  34.         current  directory,  and if it not found there,  the  search
  35.         will  continue in the "include" directory as set up  in  the
  36.         environment.   It  also uses the "less  than"  and  "greater
  37.         than" signs to indicate that the file search should begin in
  38.         the  directory  specified in the environment.  Most  of  the
  39.         programs  in  this tutorial have the double  quotes  in  the
  40.         "include" statements.  The next program uses the "<" and ">"
  41.         to  illustrate the usage.  Note that this will result  is  a
  42.         slightly  faster  (but  probably  unnoticeable)  compilation
  43.         because  the  system will not bother to search  the  current
  44.         directory.
  45.  
  46.                         INPUT/OUTPUT OPERATIONS IN C
  47.  
  48.              Actually  the  C programming language has no  input  or
  49.         output operations defined as part of the language, they must
  50.         be user defined.   Since everybody does not want to reinvent
  51.         his  own input and output operations,  the compiler  writers
  52.         have done a lot of this for us and supplied us with  several
  53.         input  functions and several output functions to aid in  our
  54.         program development.   The functions have become a standard,
  55.         and  you  will find the same functions available  in  nearly
  56.  
  57.  
  58.                                   Page 59
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.                      Chapter 9 - Standard Input/Output
  69.  
  70.  
  71.         every  compiler.   In fact,  the industry standard of the  C
  72.         language  definition has become the book written by Kernigan
  73.         and Ritchie, and they have included these functions in their
  74.         definition.   You will often,  when reading literature about
  75.         C, find a reference to K & R.  This refers to the book, "The
  76.         C  Programming Language", written by Kernigan  and  Ritchie.
  77.         You would be advised to purchase a copy for reference.
  78.  
  79.              You should print out the file named "stdio.h" and spend
  80.         some  time studying it.   There will be a lot that you  will
  81.         not understand about it, but parts of it will look familiar.
  82.         The  name  "stdio.h"  is  sort  of  cryptic  for   "standard
  83.         input/output header",  because that is exactly what it does.
  84.         It  defines  the standard input and output functions in  the
  85.         form of #defines and macros.  Don't worry too much about the
  86.         details  of this now.   You can always return to this  topic
  87.         later  for  more study if it interests  you,  but  you  will
  88.         really  have no need to completely understand the  "stdio.h"
  89.         file.  You will have a tremendous need to use it however, so
  90.         these comments on its use and purpose are necessary.
  91.  
  92.                             OTHER INCLUDE FILES
  93.  
  94.              When  you  begin writing larger programs and  splitting
  95.         them  up into separately compiled portions,  you  will  have
  96.         occasion  to  use  some  statements common to  each  of  the
  97.         portions.   It would be to your advantage to make a separate
  98.         file  containing  the  statements and use  the  #include  to
  99.         insert it into each of the files.  If you want to change any
  100.         of the common statements,  you will only need to change  one
  101.         file  and  you will be assured of having all of  the  common
  102.         statements  agree.   This  is  getting  a  little  ahead  of
  103.         ourselves  but  you  now  have  an  idea  how  the  #include
  104.         directive can be used.
  105.  
  106.                     BACK TO THE FILE NAMED "SIMPLEIO.C"
  107.  
  108.              Lets  continue our tour of the file in  question.   The
  109.         one  variable  "c" is defined and a message is  printed  out
  110.         with the familiar "printf" function.  We then find ourselves
  111.         in  a continuous loop as long as "c" is not equal to capital
  112.         X.   If  there is any question in your mind about  the  loop
  113.         control, you should review chapter 3 before continuing.  The
  114.         two  new functions within the loop are of paramount interest
  115.         in this program since they are the new functions.  These are
  116.         functions to read a character from the keyboard and  display
  117.         it on the monitor one character at a time.
  118.  
  119.              The  function "getchar()" reads a single character from
  120.         the  standard  input  device,  the  keyboard  being  assumed
  121.         because that is the standard input device, and assigns it to
  122.  
  123.  
  124.                                   Page 60
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.                      Chapter 9 - Standard Input/Output
  135.  
  136.  
  137.         the variable "c".   The next function "putchar(c)", uses the
  138.         standard output device,  the video monitor,  and outputs the
  139.         character contained in the variable "c".   The character  is
  140.         output  at  the  current cursor location and the  cursor  is
  141.         advanced  one space for the next character.   The system  is
  142.         therefore taking care of a lot of the overhead for us.   The
  143.         loop  continues reading and displaying characters  until  we
  144.         type a capital X which terminates the loop.
  145.  
  146.              Compile and run this program for a few surprises.  When
  147.         you type on the keyboard, you will notice that what you type
  148.         is displayed faithfully on the screen,  and when you hit the
  149.         return key,  the entire line is repeated.   In fact, we only
  150.         told  it  to output each character once but it seems  to  be
  151.         saving  the  characters up and redisplaying them.   A  short
  152.         explanation is in order.
  153.  
  154.                DOS IS HELPING US OUT (OR GETTING IN THE WAY)
  155.  
  156.              We need to understand a little bit about how DOS  works
  157.         to  understand  what is happening here.   When data is  read
  158.         from  the keyboard,  under DOS control,  the characters  are
  159.         stored  in  a buffer until a carriage return is  entered  at
  160.         which  time the entire string of characters is given to  the
  161.         program.   When the characters are being typed, however, the
  162.         characters are displayed one at a time on the monitor.  This
  163.         is called echo,  and happens in many of the applications you
  164.         run.
  165.  
  166.              With  the above paragraph in mind,  it should be  clear
  167.         that when you are typing a line of data into "SIMPLEIO", the
  168.         characters are being echoed by DOS,  and when you return the
  169.         carriage,  the characters are given to the program.  As each
  170.         character  is given to the program,  it displays it  on  the
  171.         screen  resulting  in  a repeat of the line  typed  in.   To
  172.         better  illustrate  this,  type  a line  with  a  capital  X
  173.         somewhere  in the middle of the line.   You can type as many
  174.         characters  as you like following the "X" and they will  all
  175.         display because the characters are being read in under  DOS,
  176.         echoed  to the monitor,  and placed in the DOS input buffer.
  177.         DOS doesn't think there is anything special about a  capital
  178.         X.   When the string is given to the program,  however,  the
  179.         characters  are  accepted by the program one at a  time  and
  180.         sent  to  the monitor one at a time,  until a capital  X  is
  181.         encountered.   After the capital X is displayed, the loop is
  182.         terminated,  and the program is terminated.   The characters
  183.         on  the input line following the capital X are not displayed
  184.         because the capital X signalled program termination.
  185.  
  186.              Compile  and  run  "SIMPLEIO.C".    After  running  the
  187.         program  several  times  and  feeling  confident  that   you
  188.  
  189.  
  190.                                   Page 61
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.                      Chapter 9 - Standard Input/Output
  201.  
  202.  
  203.         understand  the above explanation,  we will go on to another
  204.         program.
  205.  
  206.              Don't  get  discouraged by the  above  seemingly  weird
  207.         behavior  of the I/O system.   It is strange,  but there are
  208.         other ways to get data into the computer.  You will actually
  209.         find the above method useful for many applications,  and you
  210.         will probably find some of the following useful also.
  211.  
  212.                          ANOTHER STRANGE I/O METHOD
  213.  
  214.              Load  the file named SINGLEIO.C and display it on  your
  215.         monitor for another method of character I/O.  Once again, we
  216.         start  with the standard I/O header file using the  "<"  and
  217.         ">"  method of defining it. Then we define a variable  named
  218.         "c",  and  finally we print a welcoming message.   Like  the
  219.         last program, we are in a loop that will continue to execute
  220.         until  we  type  a capital X, but the  action  is  a  little
  221.         different here.
  222.  
  223.              The  "getch()"  is  a  new  function  that  is  a  "get
  224.         character" function.  It differs from "getchar()" in that it
  225.         does  not  get tied up in DOS.   It reads the  character  in
  226.         without echo, and puts it directly into the program where it
  227.         is operated on immediately.  This function therefore reads a
  228.         character,  immediately  displays  it  on  the  screen,  and
  229.         continues the operation until a capital X is typed.
  230.  
  231.              When  you compile and run this program,  you will  find
  232.         that there is no repeat of the lines when you hit a carriage
  233.         return,  and  when  you  hit  the  capital  X,  the  program
  234.         terminates immediately.  No carriage return is needed to get
  235.         it to accept the line with the X in it.   We do have another
  236.         problem  here,  however,  there  is  no  linefeed  with  the
  237.         carriage return.
  238.  
  239.                           NOW WE NEED A LINE FEED
  240.  
  241.              It  is not apparent to you in most application programs
  242.         but  when  you hit the enter key,  the  program  supplies  a
  243.         linefeed to go with the carriage return.  You need to return
  244.         to  the  left side of the monitor and you also need to  drop
  245.         down  a line.   The linefeed is not automatic.  We  need  to
  246.         improve  our program to do this also.   If you will load and
  247.         display the program named BETTERIN.C, you will find a change
  248.         to incorporate this feature.
  249.  
  250.              In BETTERIN.C, we have two additional statements at the
  251.         beginning  that  will  define the character  codes  for  the
  252.         linefeed (LF), and the carriage return (CR).  If you look at
  253.         any  ASCII table you will find that the codes 10 and 13  are
  254.  
  255.  
  256.                                   Page 62
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.                      Chapter 9 - Standard Input/Output
  267.  
  268.  
  269.         exactly  as  defined  here.   In  the  main  program,  after
  270.         outputting the character,  we compare it to CR, and if it is
  271.         equal to CR,  we also output a linefeed which is the LF.  We
  272.         could have just left out the two #define statements and used
  273.         "if  (c  ==  13)  putchar(10);" but it  would  not  be  very
  274.         descriptive  of what we are doing here.  The method used  in
  275.         the program represents better programming practice.
  276.  
  277.              Compile  and  run BETTERIN.C to see if it does what  we
  278.         have said it should do.   It should display exactly what you
  279.         type in, including a linefeed with each carriage return, and
  280.         should stop immediately when you type a capital X.
  281.  
  282.                            WHICH METHOD IS BEST?
  283.  
  284.              We have examined two methods of reading characters into
  285.         a  C program,  and are faced with a choice of which  one  we
  286.         should  use.   It really depends on the application  because
  287.         each  method has advantages and disadvantages.   Lets take a
  288.         look at each.
  289.  
  290.              When using the first method,  DOS is actually doing all
  291.         of  the  work for us by storing the characters in  an  input
  292.         buffer and signalling us when a full line has been  entered.
  293.         We  could write a program that,  for example,  did a lot  of
  294.         calculations,  then  went to get some input.   While we were
  295.         doing the calculations,  DOS would be accumulating a line of
  296.         characters  for  us,  and they would be there when  we  were
  297.         ready  for  them.   However,  we could not  read  in  single
  298.         keystrokes   because  DOS  would  not  report  a  buffer  of
  299.         characters to us until it recognized a carriage return.
  300.  
  301.              The second method, used in BETTERIN.C, allows us to get
  302.         a single character,  and act on it immediately.   We do  not
  303.         have  to  wait  until  DOS decides we can  have  a  line  of
  304.         characters.  We cannot do anything else while we are waiting
  305.         for  a  character  because  we are  waiting  for  the  input
  306.         keystroke  and tying up the entire machine.   This method is
  307.         useful  for highly interactive types of program  interfaces.
  308.         It  is up to you as the programmer to decide which  is  best
  309.         for your needs.
  310.  
  311.              I  should  mention at this point that there is also  an
  312.         "ungetch" function that works with the "getch" function.  If
  313.         you "getch" a character and find that you have gone one  too
  314.         far,  you  can "ungetch" it back to the input device.   This
  315.         simplifies  some  programs because you don't know  that  you
  316.         don't  want the character until you get it.   You  can  only
  317.         "ungetch"  one character back to the input device,  but that
  318.         is  sufficient  to  accomplish the task  this  function  was
  319.         designed for.   It is difficult to demonstrate this function
  320.  
  321.  
  322.                                   Page 63
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.                      Chapter 9 - Standard Input/Output
  333.  
  334.  
  335.         in  a simple program so its use will be up to you  to  study
  336.         when you need it.
  337.  
  338.              The discussion so far in this chapter, should be a good
  339.         indication  that,  while the C programming language is  very
  340.         flexible,  it does put a lot of responsibility on you as the
  341.         programmer to keep many details in mind.
  342.  
  343.                         NOW TO READ IN SOME INTEGERS
  344.  
  345.              Load  and display the file named INTIN.C for an example
  346.         of  reading in some formatted data.   The structure of  this
  347.         program  is  very similar to the last three except  that  we
  348.         define  an "int" type variable and loop until  the  variable
  349.         somehow acquires the value of 100.
  350.  
  351.              Instead of reading in a character at a time, as we have
  352.         in the last three files,  we read in an entire integer value
  353.         with  one  call  using the  function  named  "scanf".   This
  354.         function  is very similar to the "printf" that you have been
  355.         using for quite some time by now except that it is used  for
  356.         input instead of output.   Examine the line with the "scanf"
  357.         and  you  will notice that it does not ask for the  variable
  358.         "valin"  directly,  but  gives the address of  the  variable
  359.         since it expects to have a value returned from the function.
  360.         Recall  that a function must have the address of a  variable
  361.         in  order to return a value to that variable in the  calling
  362.         program.   Failing  to  supply  a  pointer  in  the  "scanf"
  363.         function is probably the most common problem encountered  in
  364.         using this function.
  365.  
  366.              The  function  "scanf" scans the input  line  until  it
  367.         finds  the first data field.   It ignores leading blanks and
  368.         in this case,  it reads integer characters until it finds  a
  369.         blank  or  an invalid decimal character,  at which  time  it
  370.         stops reading and returns the value.
  371.  
  372.              Remembering  our discussion above about the way the DOS
  373.         input  buffer  works,  it should be clear  that  nothing  is
  374.         actually acted on until a complete line is entered and it is
  375.         terminated by a carriage return.   At this time,  the buffer
  376.         is  input,  and  our  program will search  across  the  line
  377.         reading  all  integer values it can find until the  line  is
  378.         completely scanned.  This is because we are in a loop and we
  379.         tell it to find a value,  print it,  find another, print it,
  380.         etc.   If you enter several values on one line, it will read
  381.         each one in succession and display the values.  Entering the
  382.         value  of  100  will cause the  program  to  terminate,  and
  383.         entering  the  value 100 with other values  following,  will
  384.         cause   termination   before  the   following   values   are
  385.         considered.
  386.  
  387.  
  388.                                   Page 64
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398.                      Chapter 9 - Standard Input/Output
  399.  
  400.  
  401.  
  402.                       IT MAKES WRONG ANSWERS SOMETIMES
  403.  
  404.              If  you  enter a number up to and including  32767,  it
  405.         will display correctly, but if you enter a larger number, it
  406.         will appear to make an error.  For example, if you enter the
  407.         value 32768,  it will display the value of -32768,  entering
  408.         the  value  65536 will display as a  zero.   These  are  not
  409.         errors but are caused by the way an integer is defined.  The
  410.         most significant bit of the 16 bit pattern available for the
  411.         integer variable is the sign bit,  so there are only 15 bits
  412.         left  for the value.   The variable can therefore only  have
  413.         the  values  from  -32768 to 32767,  any  other  values  are
  414.         outside  the range of integer variables.   This is up to you
  415.         to take care of in your programs.   It is another example of
  416.         the increased responsibility you must assume using C  rather
  417.         than a higher level language such as Pascal, Modula-2, etc.
  418.  
  419.              The   above  paragraph  is  true  for  most  MS-DOS   C
  420.         compilers.   There  is a very small  possibility  that  your
  421.         compiler  uses  an integer value stored in  something  other
  422.         than 16 bits.  If that is the case, the same principles will
  423.         be true but with different limits than those given above.
  424.  
  425.              Compile and run this program,  entering several numbers
  426.         on  a line to see the results,  and with varying numbers  of
  427.         blanks between the numbers.   Try entering numbers that  are
  428.         too big to see what happens,  and finally enter some invalid
  429.         characters  to  see  what the system  does  with  nondecimal
  430.         characters.
  431.  
  432.                            CHARACTER STRING INPUT
  433.  
  434.              Load  and  display  the file named  STRINGIN.C  for  an
  435.         example  of  reading  a string variable.   This  program  is
  436.         identical to the last one except that instead of an  integer
  437.         variable,  we  have defined a string variable with an  upper
  438.         limit of 24 characters (remember that a string variable must
  439.         have  a  null character at the end).   The variable  in  the
  440.         "scanf"  does  not  need  an & because  "big"  is  an  array
  441.         variable  and by definition it is already a  pointer.   This
  442.         program  should require no additional explanation.   Compile
  443.         and run it to see if it works the way you expect.
  444.  
  445.              You probably got a surprise when you ran it because  it
  446.         separated  your sentence into separate words.   When used in
  447.         the string mode of input,  "scanf" reads characters into the
  448.         string until it comes to either the end of a line or a blank
  449.         character.   Therefore,  it  reads a word,  finds the  blank
  450.         following it,  and displays the result.   Since we are in  a
  451.         loop, this program continues to read words until it exhausts
  452.  
  453.  
  454.                                   Page 65
  455.  
  456.  
  457.  
  458.  
  459.  
  460.  
  461.  
  462.  
  463.  
  464.                      Chapter 9 - Standard Input/Output
  465.  
  466.  
  467.         the DOS input buffer.   We have written this program to stop
  468.         whenever  it  finds a capital X in column 1,  but since  the
  469.         sentence  is split up into individual words,  it  will  stop
  470.         anytime a word begins with capital X.  Try entering a 5 word
  471.         sentence  with  a  capital X as the first character  in  the
  472.         third word.  You should get the first three words displayed,
  473.         and the last two simply ignored when the program stops.
  474.  
  475.              Try  entering more than 24 characters to see  what  the
  476.         program   does.    In  an  actual  program,   it   is   your
  477.         responsibility  to count characters and stop when the  input
  478.         buffer  is full.  You may be getting the feeling that a  lot
  479.         of  responsibility is placed on you when writing in  C.   It
  480.         is,  but  you also get a lot of flexibility in  the  bargain
  481.         too.
  482.  
  483.                        INPUT/OUTPUT PROGRAMMING IN C
  484.  
  485.              C was not designed to be used as a language for lots of
  486.         input and output,  but as a systems language where a lot  of
  487.         internal operations are required.   You would do well to use
  488.         another language for I/O intensive programming,  but C could
  489.         be used if you desire.  The keyboard input is very flexible,
  490.         allowing you to get at the data in a very low level way, but
  491.         very little help is given you.  It is therefore up to you to
  492.         take  care of all of the bookkeeping chores associated  with
  493.         your  required  I/O operations.   This may seem like a  real
  494.         pain in the neck, but in any given program, you only need to
  495.         define your input routines once and then use them as needed.
  496.  
  497.              Don't let this worry you.   As you gain experience with
  498.         C, you will easily handle your I/O requirements.
  499.  
  500.              One final point must be made about these I/O functions.
  501.         It   is  perfectly  permissible  to  intermix  "scanf"   and
  502.         "getchar"  functions during read operations.   In  the  same
  503.         manner,  it  is also fine to intermix the output  functions,
  504.         "printf" and "putchar".
  505.  
  506.                                IN MEMORY I/O
  507.  
  508.              The  next operation may seem a little strange at first,
  509.         but  you will probably see lots of uses for it as  you  gain
  510.         experience.   Load the file named INMEM.C and display it for
  511.         another  type  of I/O,  one that never accesses the  outside
  512.         world, but stays in the computer.
  513.  
  514.              In INMEM.C, we define a few variables, then assign some
  515.         values to the ones named "numbers" for illustrative purposes
  516.         and then use a "sprintf" function.   The function acts  just
  517.         like  a  normal  "printf" function except  that  instead  of
  518.  
  519.  
  520.                                   Page 66
  521.  
  522.  
  523.  
  524.  
  525.  
  526.  
  527.  
  528.  
  529.  
  530.                      Chapter 9 - Standard Input/Output
  531.  
  532.  
  533.         printing the line of output to a device,  it prints the line
  534.         of  formatted  output to a character string in  memory.   In
  535.         this  case  the string goes to the string  variable  "line",
  536.         because  that  is the string name we inserted as  the  first
  537.         argument  in the "sprintf" function.   The spaces after  the
  538.         2nd  %d were put there to illustrate that the next  function
  539.         will  search  properly  across  the  line.    We  print  the
  540.         resulting  string and find that the output is  identical  to
  541.         what  it would have been by using a "printf" instead of  the
  542.         "sprintf"  in the first place.   You will see that when  you
  543.         compile and run the program shortly.
  544.  
  545.              Since  the generated string is still in memory,  we can
  546.         now  read  it  with the  function  "sscanf".   We  tell  the
  547.         function  in its first argument that "line" is the string to
  548.         use for its input,  and the remaining parts of the line  are
  549.         exactly  what  we  would  use if we were going  to  use  the
  550.         "scanf"  function and read data from outside  the  computer.
  551.         Note  that it is essential that we use pointers to the  data
  552.         because  we  want to return data from a function.   Just  to
  553.         illustrate  that  there are many ways to declare  a  pointer
  554.         several methods are used,  but all are pointers.   The first
  555.         two simply declare the address of the elements of the array,
  556.         while the last three use the fact that "result", without the
  557.         accompanying  subscript,  is  a pointer.   Just to  keep  it
  558.         interesting,  the  values  are read back in  reverse  order.
  559.         Finally the values are displayed on the monitor.
  560.  
  561.                            IS THAT REALLY USEFUL?
  562.  
  563.              It  seems sort of silly to read input data from  within
  564.         the  computer  but  it does have  a  real  purpose.   It  is
  565.         possible to read data from an input device using any of  the
  566.         standard  functions  and  then do  a  format  conversion  in
  567.         memory.   You  could read in a line of data, look at  a  few
  568.         significant  characters,  then  use  these  formatted  input
  569.         routines   to   reduce  the  line  of   data   to   internal
  570.         representation.  That would sure beat writing your own  data
  571.         formatting routines.
  572.  
  573.                            STANDARD ERROR OUTPUT
  574.  
  575.              Sometimes  it is desirable to redirect the output  from
  576.         the  standard  output device to a file.   However,  you  may
  577.         still  want the error messages to go to the standard  output
  578.         device,  in our case the monitor.  This next function allows
  579.         you to do that. Load and display SPECIAL.C for an example of
  580.         this new function.
  581.  
  582.              The  program  consists  of a  loop  with  two  messages
  583.         output,  one  to the standard output device and the other to
  584.  
  585.  
  586.                                   Page 67
  587.  
  588.  
  589.  
  590.  
  591.  
  592.  
  593.  
  594.  
  595.  
  596.                      Chapter 9 - Standard Input/Output
  597.  
  598.  
  599.         the  standard  error device.   The message to  the  standard
  600.         error  device  is  output with the  function  "fprintf"  and
  601.         includes  the  device name "stderr" as the  first  argument.
  602.         Other  than those two small changes,  it is the same as  our
  603.         standard  "printf"  function.   (You will see  more  of  the
  604.         "fprintf"  function in the next chapter,  but its  operation
  605.         fit  in better as a part of this chapter.)  Ignore the  line
  606.         with the "exit" for the moment, we will return to it.
  607.  
  608.              Compile  and  run this program,  and you will  find  12
  609.         lines of output on the monitor.   To see the difference, run
  610.         the  program  again with redirected output to a  file  named
  611.         "STUFF" by entering the following line at the Dos prompt;
  612.  
  613.         A> special >stuff
  614.  
  615.              More  information about I/O redirection can be found in
  616.         your  DOS manual.   This time you will only get the 6  lines
  617.         output to the standard error device, and if you look in your
  618.         directory,  you will find the file named "STUFF"  containing
  619.         the other 6 lines, those to the standard output device.  You
  620.         can use I/O redirection with any of the programs we have run
  621.         so far,  and as you may guess, you can also read from a file
  622.         using I/O redirection but we will study a better way to read
  623.         from a file in the next chapter.
  624.  
  625.                      WHAT ABOUT THE exit(4) STATEMENT?
  626.  
  627.              Now  to  keep our promise about the exit(4)  statement.
  628.         Redisplay  the file named SPECIAL.C on  your  monitor.   The
  629.         last  statement  simply  exits the program and  returns  the
  630.         value  of 4 to DOS.   Any number from 0 to 9 can be used  in
  631.         the parentheses for DOS communication.  If you are operating
  632.         in  a  BATCH  file,  this  number can  be  tested  with  the
  633.         "ERRORLEVEL" command.
  634.  
  635.              Most compilers that operate in several passes return  a
  636.         1  with  this mechanism to indicate that a fatal  error  has
  637.         been  detected and it would be a waste of time to go  on  to
  638.         another pass resulting in even more errors.
  639.  
  640.              It is therefore wise to use a batch file for compileing
  641.         programs and testing the returned value for errors.  A check
  642.         of  the documentation for my COMPAQ, resulted in  a  minimal
  643.         and confusing documentation of the "ERRORLEVEL" command,  so
  644.         a brief description of it is given in this file in case your
  645.         documentation  does not include enough information to  allow
  646.         you to use it.
  647.  
  648.  
  649.  
  650.  
  651.  
  652.                                   Page 68
  653.  
  654.  
  655.  
  656.  
  657.  
  658.  
  659.  
  660.  
  661.  
  662.                      Chapter 9 - Standard Input/Output
  663.  
  664.  
  665.         PROGRAMMING EXERCISE
  666.  
  667.         1.   Write  a program to  read in a character using a  loop,
  668.              and  display the character in its normal  "char"  form.
  669.              Also  display  it  as a decimal  number.  Check  for  a
  670.              dollar  sign  to use as the  stop  character.  Use  the
  671.              "getch" form of input so it will print immediately. Hit
  672.              some of the special keys,  such as function keys,  when
  673.              you  run the program for some surprises.  You will  get
  674.              two  inputs  from the special keys,  the first being  a
  675.              zero  which  is  the indication to the  system  that  a
  676.              special key was hit.
  677.  
  678.  
  679.  
  680.  
  681.  
  682.  
  683.  
  684.  
  685.  
  686.  
  687.  
  688.  
  689.  
  690.  
  691.  
  692.  
  693.  
  694.  
  695.  
  696.  
  697.  
  698.  
  699.  
  700.  
  701.  
  702.  
  703.  
  704.  
  705.  
  706.  
  707.  
  708.  
  709.  
  710.  
  711.  
  712.  
  713.  
  714.  
  715.  
  716.  
  717.  
  718.                                   Page 69
  719.