home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_09_06 / 9n06120a < prev    next >
Text File  |  1991-04-23  |  2KB  |  92 lines

  1. #include  <stdio.h>
  2. #include  <stdlib.h>
  3.  
  4. FILE      *testfile;
  5.  
  6. float     r();
  7. int       w();
  8.  
  9. main()
  10.      {
  11.      float     f;
  12.      int       rc;
  13.  
  14.      /* open test file */
  15.      testfile = fopen("test.dat", "rb+");
  16.      if (testfile == NULL)
  17.           {
  18.           printf("MAIN : open error\n");
  19.           return;
  20.           }
  21.      printf("MAIN : \tf = %f\n", f);
  22.      
  23.      /* call subroutine to read the floating point number */
  24.      f = r();
  25.  
  26.      /* call subroutine to write a floating point number */
  27.      f = 123.45;
  28.      w(f);
  29.  
  30.      /* call subroutine to read the floating point number */
  31.      f = r();
  32.  
  33.      fclose (testfile);
  34.      return;
  35.  
  36.      }
  37.  
  38. /****************************************************************
  39. * subroutine R : read a floating point number from test file
  40. ****************************************************************/
  41. float
  42. r()
  43.      {
  44.      float     f;
  45.      int       rc;
  46.      rewind(testfile);
  47.      rc = fread(&f, sizeof(float), 1, testfile);
  48.      if (rc != 1)
  49.           {
  50.           printf("R : read error\n");
  51.           exit(0);
  52.           }
  53.      printf("R : ;\tf = %f\n", f);
  54.      return f;
  55.  
  56.      }
  57.  
  58. /***************************************************************
  59. * subroutine W : write a floating point number in test file
  60. ***************************************************************/
  61. int
  62. w(flo)
  63. float     flo;
  64.      {
  65.      float     f;
  66.      int       rc;
  67.      
  68.      f = flo;
  69.  
  70.      printf("W : \tf = %fzn", f);
  71.  
  72.      rewind(testfile);
  73.      rc = fwrite(&f, sizeof(float), 1, testfile);
  74.      if (rc != 1)
  75.           {
  76.           printf("W : write error\n");
  77.           exit(0);
  78.           }
  79.  
  80.      return 1;
  81.      }
  82. /********
  83. * OUTPUT
  84. *********
  85. MAIN :    f = 123.449997
  86. R :       f = 123.449997
  87. W :       f = 123.450000
  88. R :       f = 123.449997
  89.  
  90. */
  91.  
  92.