home *** CD-ROM | disk | FTP | other *** search
/ C by Discovery (4th Edition) / C_By_Discovery_4th_Edition.tar / C_By_Discovery_4th_Edition / _DISK_ / ch10 / ungetc.c < prev   
C/C++ Source or Header  |  2005-06-16  |  2KB  |  45 lines

  1. /*               ungetc.c
  2.  *
  3.  *   Synopsis  - Picks out all strings of alphabetic characters
  4.  *               and all strings of digits from "text" and prints
  5.  *               each on a separate line of terminal output.
  6.  *
  7.  *   Objective - Illustrates the use of the file facilities
  8.  *               ungetc(), feof() and the character typing 
  9.  *               facilities isalpha() and isdigit().
  10.  */
  11.  
  12. /* Include Files */
  13. #include <stdio.h>
  14. #include <ctype.h>                                     /* Note 1 */
  15. #include <stdlib.h>
  16.  
  17. int main( void )
  18. {
  19.      FILE *fp;
  20.      int iochar;
  21.  
  22.      if (( fp = fopen( "text", "r" )) == NULL ) {
  23.           printf( "text couldn't be opened\n" );
  24.           exit( 1 );
  25.      }
  26.  
  27.      while ( !feof( fp )) {                            /* Note 2 */
  28.                                                        /* Note 3 */
  29.           while ( isalpha( iochar = getc( fp )))
  30.                putchar( iochar );
  31.           putchar( '\n' );
  32.           ungetc( iochar, fp );                        /* Note 4 */
  33.                                                        /* Note 5 */
  34.           while ( isdigit( iochar = getc( fp )))
  35.                putchar( iochar );
  36.           putchar( '\n' );
  37.           ungetc( iochar, fp );                        /* Note 6 */
  38.                                                        /* Note 7 */
  39.           while ( !isalpha( iochar = getc( fp )) && ( !isdigit( iochar )) && ( !feof( fp )))
  40.           ;
  41.           ungetc( iochar, fp );                        /* Note 8 */
  42.      }
  43.      fclose( fp );
  44.      return 0;
  45. }