home *** CD-ROM | disk | FTP | other *** search
/ Piper's Pit BBS/FTP: ibm 0000 - 0009 / ibm0000-0009 / ibm0003.tar / ibm0003 / LCNOW2.ZIP / EXAMPLES / WRITE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-08-03  |  813 b   |  46 lines

  1. /*
  2.  * W R I T E
  3.  *
  4.  * Write a line of text to a file.
  5.  */
  6.  
  7. #include <stdio.h>
  8.  
  9. #define MAXPATH 64
  10.  
  11. int
  12. main(void)
  13. {
  14.     int ch;                 /* input character */
  15.     FILE *fp;               /* file pointer */
  16.     char pathname[MAXPATH]; /* filename buffer */
  17.  
  18.     /*
  19.      * Prompt the user for a filename and read it.
  20.      */
  21.     printf("Filename: ");
  22.     gets(pathname);
  23.     if (*pathname == '\0')  /* no name typed */
  24.         return (0);
  25.  
  26.     /*
  27.      * Open the named file for writing.
  28.      */
  29.     fp = fopen(pathname, "w");
  30.  
  31.     /*
  32.      * Read characters from the keyboard up to a newline
  33.      * character, then write the line to the specified file.
  34.      */
  35.     while ((ch = getchar()) != '\n')
  36.         fputc(ch, fp);
  37.     fputc('\n', fp);        /* terminate the line */
  38.  
  39.     /*
  40.      * Close the file.
  41.      */
  42.     fclose(fp);
  43.  
  44.     return (0);
  45. }
  46.