#include #include #include using namespace std; bool is_word_in_array(string word, string array[], int len); int read_file(ifstream & din, string coll[], int arrsize) // & din is not a copy of book, it actually // is book, the one and only, itself. { int count = 0; // while (book.eof() == false) no no no. test after attempting. while (true) { string w; din >> w; if (din.fail()) break; if (count >= arrsize) { cerr << "array too small\n"; return count; } if (is_word_in_array(w, coll, count) == false) { coll[count] = w; count += 1; } } return count; } void print_array(string a[], int len) { for (int i = 0; i < len; i += 1) cout << i << ": " << a[i] << "\n"; } bool is_word_in_array(string word, string array[], int len) { for (int i = 0; i < len; i += 1) if (array[i] == word) return true; return false; } int main() { const int maxsize = 41000; string content[maxsize]; // a bit of a cheat, pre-reading file ifstream book("/home/www/text/buchan/thirtyninesteps"); if (book.fail()) { cerr << "Can't open file\n"; exit(1); } int count = read_file(book, content, maxsize); cout << "there were " << count << " words\n"; print_array(content, count); book.close(); }