/* The task: write a program, showfroot.c, that displays each record * in the froot.txt file. Use fread() to read in each file chunk. Have * each chunk displayed along with its record number using this * format: "14: Melon". */ #include int main() { int i = 0; int prev; int curr = 0; int record_number = 0; int char_counter = 0; int record_length; /* Reading the file */ FILE *the_file; the_file = fopen("froot.txt", "r"); if(!the_file) { puts("Error reading file"); return(1); } /* Figuring out the number of records by counting * "non-null -- null" combinations, printing * statistics */ while(curr != EOF) { prev = curr; curr = fgetc(the_file); if(prev != 0 && curr == 0) record_number++; char_counter++; } record_length = char_counter / record_number; printf("There are %d records in the file, %d characters each: \n", record_number, record_length); rewind(the_file); char froot[record_number][record_length]; /* reading the records */ fread(froot, record_length, record_number, the_file); /* printing the records */ for(i = 0; i < record_number; i++) { if(i > 0 && i % 5 == 0) putchar('\n'); printf("%2d: %-*s", i + 1, record_length, froot[i]); } putchar('\n'); return(0); } /* There are 25 records in the file, 14 characters each: * 1: Apple 2: Avocado 3: Banana 4: Blackberry 5: Boysenberry * 6: Cantaloupe 7: Cherry 8: Coconut 9: Cranberry 10: Cumquat * 11: Grape 12: Guava 13: Mango 14: Marionberry 15: Melon * 16: Orange 17: Papaya 18: Peach 19: Pear 20: Persimmon * 21: Pineapple 22: Plum 23: Raspberry 24: Strawberry 25: Watermelon */