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

  1. /*
  2.  * W R I T E _ 2
  3.  *
  4.  * Write line of text to a file. Terminate input by
  5.  * pressing Enter on a blank line.
  6.  */
  7.  
  8. #include <stdio.h>
  9.  
  10. #define MAXPATH 64
  11. #define MAXLINE 256
  12.  
  13. int
  14. main(void)
  15. {
  16.     int ch;                 /* input character */
  17.     FILE *fp;               /* file pointer */
  18.     char pathname[MAXPATH]; /* file name buffer */
  19.     char line[MAXLINE];     /* line buffer */
  20.  
  21.     /*
  22.      * Prompt the user for a filename and read it.
  23.      */
  24.     printf("Filename: ");
  25.     gets(pathname);
  26.     if (*pathname == '\0')  /* no name typed */
  27.         return (0);
  28.  
  29.     /*
  30.      * Open the named file for writing.
  31.      */
  32.     fp = fopen(pathname, "w");
  33.  
  34.     /*
  35.      * Read lines of text from the keyboard and write them to
  36.      * the specified file. Quit when an empty line is seen.
  37.      */
  38.     while (1) {
  39.         fgets(line, MAXLINE, stdin);
  40.         if (line[0] == '\n')    /* empty line */
  41.             break;
  42.         fputs(line, fp);
  43.     }
  44.  
  45.     /*
  46.      * Close the file.
  47.      */
  48.     fclose(fp);
  49.  
  50.     return (0);
  51. }
  52.