/* Make a floating point value in a file, suitable for dumping. * * Usage: $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! D. Allen - idallen@idallen.ca - www.idallen.com */ #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 number argument.\n", argv[0]); return(1); } f = atof(argv[1]); /* NOTE: atof has no error checking! */ printf("I found number '%f'\n", f); fd = fopen(FILENAME,"wb"); /* write binary format */ 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; } -- | Ian! D. Allen - idallen@idallen.ca - Ottawa, Ontario, Canada | Home Page: http://idallen.com/ Contact Improv: http://contactimprov.ca/ | College professor (Free/Libre GNU+Linux) at: http://teaching.idallen.com/ | Defend digital freedom: http://eff.org/ and have fun: http://fools.ca/