#include <stdio.h> #include <stdlib.h> int main( void ){ char file[] = "tmp.bin"; printf("Open %s\n", file); FILE *fp = fopen( file, "rb" ); if( fp == NULL ){ fputs("Failed to open the file.\n", stderr); exit( EXIT_FAILURE ); } int n; double x; char str[4]; if( fread( &n, sizeof(n), 1, fp ) < 1 ){ fputs( "Failed to write data.\n", stderr ); exit( EXIT_FAILURE ); } if( fread( &x, sizeof(x), 1, fp ) < 1 ){ fputs( "Failed to write data.\n", stderr ); exit( EXIT_FAILURE ); } if( fread( str, sizeof(str[0]), sizeof(str), fp ) < sizeof(str) ){ fputs( "Failed to write data.\n", stderr ); exit( EXIT_FAILURE ); } printf("Read: n = %d, x = %f, str = %s\n", n, x, str); printf("Close the file.\n"); if( fclose(fp) == EOF ){ fputs( "Failed to close the file.\n", stderr ); exit( EXIT_FAILURE ); } return 0; }