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