/************************************************************************/ /* This is an example of a working trans.c program. It receives input */ /* from stdin, and outputs to a binary file specified at the command */ /* line. */ /* */ /* Please note: your solutions are expected to be commented thoroughly, */ /* and make appropriate use of functions and #define'd constants. */ /************************************************************************/ #include /* The record structure */ #define STR_LEN 256 struct master { long id; int fn; char df[9]; int nc; char lc[5][15]; int ns; int ncr; char cnm[5][10]; char type; } master; main(int argc, char *argv[]) { char data[STR_LEN]; int i,j; FILE *fp; char* outfile= argv[1]; fp = fopen(outfile,"w"); for (i = 0; i < 1; i++) { /* read one record */ gets(data); master.id = atol(data); gets(data); master.fn = atoi(data); gets(data); strncpy(&master.df, data, 8); gets(data); master.nc = atoi(data); for (j = 0; j < master.nc; j++) { gets(data); strncpy(&master.lc[j], data, 15); master.lc[j][14] = '\0'; } gets(data); master.ns = atoi(data); gets(data); master.ncr = atoi(data); for (j = 0; j < master.ncr; j++) { gets(data); strncpy(&master.cnm[j], data, 10); master.cnm[j][9] = '\0'; } gets(data); master.type = (data[0]); fwrite(&master, sizeof(master), 1, fp); } fclose(fp); fp = fopen(outfile,"r"); /* output record to stdout, useful for generating text files... */ for (i = 0; i < 1; i++) { fread(&master, sizeof(master), 1, fp); printf("%ld \n",master.id); printf("%d \n",master.fn); printf("%-8s \n",master.df); printf("%d \n",master.nc); for (j = 0; j < master.nc; j++) printf("%-15s \n", master.lc[j]); printf("%d \n",master.ns); printf("%d \n",master.ncr); for (j = 0; j < master.ncr; j++) printf("%-10s \n",master.cnm[j]); printf("%c \n",master.type); } fclose(fp); } /* end of trans.c */