/* Make a floating point value in a file. * * $0 number * * Given a number on the command line, create a file FLOAT.BIN * containing the given number in floating point format, suitable * for dumping and decoding. The byte order of the current hardware * is used. -IAN! idallen@ncf.ca */ #include #include #include #define FILENAME "FLOAT.BIN" int main(int argc, char **argv){ float f; FILE *fd; int nrec; if ( argc != 2 ) { fprintf(stderr,"%s: Expecting exactly one argument.\n", argv[0]); return(1); } f = atof(argv[1]); printf("I found number '%f'\n", f); fd = fopen(FILENAME,"wb"); if ( fd == NULL ) { perror(FILENAME); fprintf(stderr,"%s: Unable to open file '%s' for writing.\n", argv[0], FILENAME); return(1); } nrec = fwrite(&f,sizeof(f),1,fd); if ( nrec != 1 ) { perror(FILENAME); fprintf(stderr,"%s: Unable to write one item to '%s'.\n", argv[0], FILENAME); return(1); } if( fclose(fd) != 0 ){ perror(FILENAME); fprintf(stderr,"%s: Unable to close file '%s'.\n", argv[0], FILENAME); return(1); } printf("I wrote %d bytes to file '%s'.\n", nrec * sizeof(f), FILENAME); return 0; }