/*
 * ASCII to FLOAT converter (ANSI C).
 *
 * by Gabriele Falcioni.
 * This source is Public Domain.
 */

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>

#define STREQ(s1,s2) (!strcmp((s1),(s2)))
#define STRBUFLEN 256

char strbuf[STRBUFLEN];

void ascii2float(FILE *sf,FILE *df)
{
	register char *tk;
	register long nn;
	float fpnum;

	printf("conversion in progress...");
	fflush(stdout);

	nn = 0;
	while (fgets(strbuf,STRBUFLEN,sf)) {

		tk = strbuf;
		while (tk = strtok(tk," \t\n")) {

			fpnum = atof(tk);
			if (fwrite(&fpnum,sizeof(float),1,df) != 1) {
				perror("\nerror while writing");
				return;
			}

			++nn;
			tk = NULL;
		}
	}

	printf("\nconverted %d number%s\n",nn,
		(nn == 1 ? "" : "s") );
}

void main(int argc,char *argv[])
{
	FILE *sf,*df;

	if (argc != 3) {
		printf("Usage: %s infile outfile\n",argv[0]);
		exit(0);
	}

	if (sf = fopen(argv[1],"r")) {

		if (df = fopen(argv[2],"wb")) {

			ascii2float(sf,df);

			fclose(df);
		} else perror("error while opening target file");

		fclose(sf);
	} else perror("error while opening source file");
}
