Listing 1 (test1.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;
          }

     /* write a floating point number in the file */
     f = 123.45;
     rewind(testfile);
     rc = fwrite(&f, sizeof(float), 1, testfile);
     if (rc != 1)
          {
          printf("MAIN : write error\n");
          return;
          }
     printf("MAIN : \tf = %f\n", f);

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

     /* call subroutine to write the 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(f)
float      f;
     {
     int       rc;   /* return code */

     printf("W : \tf = %f\n", 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 = -107374184.000000

*/

/* End of File */