This is not homework.. I am just stuck at a place where I have no access to a c compiler and I need to verify that this code will actually compile and run before I send it off. Any help would be appreciated.
Sample data in infile:
Output should be:Code:1:ABC:07 2:DEF:10 2:ABD:05 1:CDF:12
Code:outfile1: ABC CDF 19 outfile2: DEF ABD 15Code:#include <stdio.h> #include <stdlib.h> #include <string.h> FILE *input_file, *outfile1, *outfile2; int main(void) { char file_line[8]; int total1, total2; char *temp; input_file = fopen("infile", "rt"); outfile1 = fopen("outfile1", "wt"); outfile2 = fopen("outfile2", "wt"); total1 = 0; total2 = 0; if (input_file == NULL) { printf ("** Data file could not be opened **"); } else { while(fgets(file_line, 8, input_file) != NULL) { temp = strtok(file_line, ":"); if (atoi(temp[0]) == 1) { fprintf(outfile1, "%s\n", temp[1]); total1 = total1 + atoi(temp[2]); } else if (atoi(temp[0]) == 2) { fprintf(outfile2, "%s\n", temp[1]); total2 = total2 + atoi(temp[2]); } else { printf("Unexpected data in field 1!\n"); } } fprintf(outfile1, "%u\n", total1); fprintf(outfile2, "%u\n", total2); } }



1Likes
LinkBack URL
About LinkBacks


