home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_09_06 / 9n06118a < prev    next >
Text File  |  1991-04-23  |  2KB  |  98 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.  
  22.      /* write a floating point number in the file */
  23.      f = 123.45;
  24.      rewind(testfile);
  25.      rc = fwrite(&f, sizeof(float), 1, testfile);
  26.      if (rc != 1)
  27.           {
  28.           printf("MAIN : write error\n");
  29.           return;
  30.           }
  31.      printf("MAIN : \tf = %f\n", f);
  32.  
  33.      /* call subroutine to read the floating point number */         
  34.      f = r();
  35.  
  36.      /* call subroutine to write the floating point number */
  37.      f = 123.45;
  38.      w(f);
  39.      /* call subroutine to read the floating point number */
  40.      f = r();
  41.  
  42.      fclose (testfile);
  43.      return;
  44.      }
  45.  
  46. /****************************************************************
  47. * subroutine R : read a floating point number from test file
  48. *****************************************************************/
  49. float
  50. r()
  51.      {
  52.      float     f;
  53.      int  rc;
  54.      
  55.      rewind(testfile);
  56.      rc = fread(&f, sizeof(float), 1, testfile);
  57.      if (rc !=1)
  58.           {
  59.           printf("R : read error\n");
  60.           exit(0);
  61.           }
  62.      printf("R : \tf = %f\n", f);
  63.      return f;
  64.      }
  65.  
  66. /****************************************************************
  67. * subroutine W : write a floating point number in test file
  68. ****************************************************************/
  69. int
  70. w(f)
  71. float     f;
  72.      {
  73.      int       rc;  /* return code */
  74.  
  75.      printf("W : \tf = %f\n", f);
  76.      rewind(testfile);
  77.      rc = fwrite(&f, sizeof(float), 1, testfile);
  78.      if (rc != 1)
  79.           {
  80.           printf("W : write error\n");
  81.           exit(0);
  82.           }
  83.      
  84.      return 1;
  85.      }
  86.  
  87.  
  88. /*********
  89. * OUTPUT
  90. **********
  91. MAIN :    f = 123.449997
  92. R :       f = 123.449997
  93. W :       f = 123.450000
  94. R :       f = -107374184.000000
  95.  
  96. */
  97.  
  98.