/* BLOCK.C ** -- program to make a dataset rectangular ** -- Phillip Swagel (os14: STB-20) */ #include void main(int argc, char *arg[]) { int columns = -1; int colnow = 0; long rownow = 0; long totalread = 0; int i = -1; int j = -1; char infile[80], outfile[80], instring[100]; FILE *fpin, *fpout; while (i < 1) { printf("\nName of the input file: "); i = scanf("%s",infile); fpin = fopen(infile,"r"); if (fpin == NULL) { printf("Can't find %s",infile); i = -1; } } while (j < 1) { printf("\nName of the output file: "); j = scanf("%s", outfile); fpout = fopen(outfile,"w"); if (fpout == NULL) { printf("Can't create %s",outfile); j = -1; } } while (columns < 1) { printf("\nNumber of columns: "); scanf("%d", &columns); } fscanf(fpin,"%s",instring); while (!feof(fpin)) { totalread++; fprintf(fpout,"%s ",instring); colnow++; if (colnow >= columns) { fprintf(fpout,"\n"); colnow = 0; rownow++; printf("."); } fscanf(fpin,"%s",instring); } fclose(fpin); fclose(fpout); printf("\nRead in %ld fields from %s\n",totalread,infile); printf("Wrote out %ld rows of %d columns to %s\n",rownow,columns,outfile); if ( (rownow * (long) columns) != totalread ) printf("\nWARNING: last row not complete."); }