// Use this minimalist program to test your asn 9 disk hash. // It just runs through the hash_file sequentially so there is no lseek(). // It does shortcut a bit on the read tests - so long as it reads more than // 0 bytes it presumes that it got the whole hentry record. Aside from disk // block errors this is actually safe if the hash_file is properly formed. #include #define MAXWLEN 24 struct hentry { int count; char word[MAXWLEN]; } hentry; // A single reusable hentry struct char most_frequent_word[MAXWLEN]; int most_frequent_count, total, unique; main() { int h = 0, fd = open("hash_file", 0); // open the hash_file for(h = 0; read(fd, &hentry, sizeof(struct hentry)) > 0; h++) { if(hentry.count == 0) continue; //printf("%d\t%s\t%d\n", h, hentry.word, hentry.count);// if you want to print unique++; total += hentry.count; if(hentry.count > most_frequent_count) { most_frequent_count = hentry.count; strcpy(most_frequent_word, hentry.word); } } printf("%d unique words out of %d total words\n", unique, total); printf("most frequent word <%s> appears %d times\n", most_frequent_word, most_frequent_count); }