home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c021 / 7.img / EXAMPLES.ZIP / INTRO34.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-05-04  |  970 b   |  40 lines

  1. /* INTRO34.C--Example from Chapter 4 of Getting Started */
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6. FILE *textfile;        /* Pointer to file being used */
  7. char line[81];         /* Char array to hold lines read from file */
  8.  
  9. int main()
  10. {
  11.    /* Open file, testing for success */
  12.    if ((textfile = fopen("intro34.txt", "w")) == NULL) {
  13.       printf("Error opening text file for writing\n");
  14.       exit(0);
  15.    }
  16.  
  17.    /* Write some text to the file */
  18.    fprintf(textfile, "%s\n", "one");
  19.    fprintf(textfile, "%s\n", "two");
  20.    fprintf(textfile, "%s\n", "three");
  21.  
  22.    /* Close the file */
  23.    fclose(textfile);
  24.  
  25.    /* Open file again */
  26.    if ((textfile = fopen("intro34.txt", "r")) == NULL) {
  27.       printf("Error opening text file for reading\n");
  28.       exit(0);
  29.    }
  30.  
  31.    /* Read file contents */
  32.    while ((fscanf(textfile, "%s", line) != EOF))
  33.       printf("%s\n", line);
  34.  
  35.    /* Close file */
  36.    fclose(textfile);
  37.  
  38.    return 0;
  39. }
  40.