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

  1.  /* Demonstrates the fprintf() function. */
  2.  
  3.  #include <stdio.h>
  4.  
  5.  void clear_kb(void);
  6.  
  7.  main()
  8.  {
  9.      FILE *fp;
  10.      float data[5];
  11.      int count;
  12.      char filename[20];
  13.  
  14.      puts("Enter 5 floating point numerical values.");
  15.  
  16.      for (count = 0; count < 5; count++)
  17.          scanf("%f", &data[count]);
  18.  
  19.      /* Get the filename and open the file. First clear stdin */
  20.      /* of any extra characters. */
  21.  
  22.      clear_kb();
  23.  
  24.      puts("Enter a name for the file.");
  25.      gets(filename);
  26.  
  27.      if ( (fp = fopen(filename, "w")) == NULL)
  28.      {
  29.          fprintf(stderr, "Error opening file %s.", filename);
  30.          exit(1);
  31.      }
  32.  
  33.      /* Write the numerical data to the file and to stdout. */
  34.  
  35.      for (count = 0; count < 5; count++)
  36.      {
  37.          fprintf(fp, "\ndata[%d] = %f", count, data[count]);
  38.          fprintf(stdout, "\ndata[%d] = %f", count, data[count]);
  39.      }
  40.  
  41.      fclose(fp);
  42.  }
  43.  
  44.  void clear_kb(void)
  45.  /* Clears stdin of any waiting characters. */
  46.  {
  47.      char junk[80];
  48.      gets(junk);
  49.     }
  50.