#include <stdio.h>
#include <stdlib.h>

int
main(int argc, char **args)
{
	FILE *fIn=NULL;		/* input file handle */
	FILE *fOut;		/* output file handle */
	int readValue;		/* value read from input file */
	int columnCount;	/* for counting # of items per line */
	int charCount;		/* # of characters read so far */

	/* can only do this if we have both file names */
	if (argc!=3) {
		fprintf(stderr, "usage: %s INFILE OUTFILE\n",
		  args[0]);
		exit(1);
	}

	/* open up file for reading */
	if ((fIn=fopen(args[1], "r"))==NULL) {
		fprintf(stderr, "%s: Error opening %s for reading\n",
		args[0], args[1]);
		exit(1);
	}

	/* open up destination file for writing */
	if ((fOut=fopen(args[2], "w"))==NULL) {
		fprintf(stderr, "%s: Error opening %s for writing\n",
		args[0], args[2]);
		exit(1);
	}
	
	columnCount=0;
	charCount=0;
	fprintf(fOut, "{ {");
	do {
		readValue=fgetc(fIn);
		if (!feof(fIn)) {
			fprintf(fOut, "%d", readValue);
			columnCount++;
			if (columnCount==8) {
				columnCount=0;
				charCount++;
				if (charCount != 256) {
					fprintf(fOut, "},\n{");
				} else {
					fprintf(fOut, "}\n");
				}
				fprintf(stdout, "%d ", charCount);
				fflush(stdout);
			} else {
				fprintf(fOut, ",");
			}
		}
	} while (!feof(fIn));

	if (columnCount != 0) {
		fprintf(fOut, "}");
	}
	fprintf(fOut, "}");
	fclose(fIn);
	fclose(fOut);
	fprintf(stdout, "done\n");
	return 0;
}



