home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / tyc / list16_1.c < prev    next >
C/C++ Source or Header  |  1993-10-16  |  1KB  |  45 lines

  1.  /* Demonstrates the fopen() function. */
  2.  
  3.  #include <stdio.h>
  4.  
  5.  main()
  6.  {
  7.      FILE *fp;
  8.      char ch, filename[40], mode[4];
  9.  
  10.      while (1)
  11.      {
  12.  
  13.          /* Input filename and mode. */
  14.  
  15.          printf("\nEnter a filename: ");
  16.          gets(filename);
  17.          printf("\nEnter a mode (max 3 characters): ");
  18.          gets(mode);
  19.  
  20.          /* Try to open the file. */
  21.  
  22.          if ( (fp = fopen( filename, mode )) != NULL )
  23.          {
  24.              printf("\nSuccessful opening %s in mode %s.\n",
  25.                      filename, mode);
  26.              fclose(fp);
  27.              puts("Enter x to exit, any other to continue.");
  28.              if ( (ch = getch()) == 'x')
  29.                  break;
  30.              else
  31.                  continue;
  32.          }
  33.          else
  34.          {
  35.              fprintf(stderr, "\nError opening file %s in mode %s.\n",
  36.                      filename, mode);
  37.              puts("Enter x to exit, any other to try again.");
  38.              if ( (ch = getch()) == 'x')
  39.                  break;
  40.              else
  41.                  continue;
  42.          }
  43.      }
  44.  }
  45.