Listing 2 (test2.c)

#include  <stdio.h>
#include  <stdlib.h>

FILE      *testfile;

float     r();
int       w();

main()
     {
     float  f;
     int  rc;

     /* open test file */
     testfile = fopen("test.dat", "rb+");
     if (testfile == NULL)
          {
          printf("MAIN : open error\n");
          return;
          }
     printf("MAIN : \tf = %f\n", f);

     /* call subroutine to read the floating point number */
     f = r();

     /* call subroutine to write a floating point number */
     f = 123.45;
     w(f);

     /* call subroutine to read the floating point number */
     f = r();

     fclose (testfile);
     return;

     }

/*************************************************************
* subroutine R : read a floating point number from test file
*************************************************************/
float
r()
     {
     float      f;
     int        rc;
     rewind(testfile);
     rc = fread(&f, sizeof(float), 1, testfile);
     if (rc != 1)
          {
          printf("R : read error\n");
          exit(0);
          }
     printf("R : ;\tf = %f\n", f);
     return f;

     }

/***************************************************************
* subroutine W : write a floating point number in test file
**************************************************************/
int
w(flo)
float      flo;
     {
     float     f;
     int       rc;

     f =  flo;

     printf("W : \tf = %fzn", f);

     rewind(testfile);
     rc = fwrite(&f, sizeof(float), 1, testfile);
     if (rc != 1)
          {
          printf("W : write error\n");
          exit(0);
          }

     return 1;
     }
/********
* OUTPUT
*********
MAIN :     f = 123.449997
R :         f = 123.449997
W :         f = 123.450000
R :         f = 123.449997

*/