home *** CD-ROM | disk | FTP | other *** search
/ Jason Aller Floppy Collection / 98.img / LCNOW2.ZIP / EXAMPLES / LIST_3.C < prev    next >
C/C++ Source or Header  |  1988-08-03  |  899b  |  50 lines

  1. /*
  2.  * L I S T _ 3
  3.  *
  4.  * Display the contents of an ASCII text file
  5.  * file on the user's screen.
  6.  *
  7.  * NOTE: This version uses string functions.
  8.  */
  9.  
  10. #include <stdio.h>
  11.  
  12. #define MAXPATH 64
  13. #define MAXLINE 256
  14.  
  15. int
  16. main(void)
  17. {
  18.     int ch;                 /* input character */
  19.     FILE *fp;               /* file pointer */
  20.     char pathname[MAXPATH]; /* filename buffer */
  21.     char line[MAXLINE];     /* line buffer for fgets() */
  22.  
  23.     /*
  24.      * Prompt the user for a filename and read it.
  25.      */
  26.     printf("Filename: ");
  27.     gets(pathname);
  28.     if (*pathname == '\0')
  29.         return (0);
  30.  
  31.     /*
  32.      * Open the named file for reading.
  33.      */
  34.     fp = fopen(pathname, "r");
  35.  
  36.     /*
  37.      * Read the contents of the file and display it
  38.      * a line at a time as it is read.
  39.      */
  40.     while (fgets(line, MAXLINE, fp) != NULL)
  41.         fputs(line, stdout);
  42.  
  43.     /*
  44.      * Close the file.
  45.      */
  46.     fclose(fp);
  47.  
  48.     return (0);
  49. }
  50.