// File name: filechk.cpp // Author: Xiannong Meng // Course: CS2330 // Date: February 27, 1997 // Assignment: classroom discussion // Problem statement: This program takes a word from user, checks against // a dictionary, if the word is not found in the dictionary, it is // inserted into the dictionary in a proper place. #include #include #include #include // for getch() #include "./cstring/cstring.h" void main(void) { void SearchWord(String w); ifstream src("words.txt"); // text file to test spell check String word; if (src) { src >> word; while (!src.eof()) { SearchWord(word); src >> word; } } else cout << " file open failed \n"; } // Name : SerachWord // Intent: search a word in the dictionary // Pre: a word is given // Post: the word is found in the dictionary, // or, asking if the word needs to be inserted // into the dictionary void SearchWord(String word) { ifstream inF("words"); // original dictionary is "words" ofstream outF("words.tmp"); // new dictionary is "words.tmp" String compWord; int changed = 0; inF >> compWord; while ((inF) && (compWord < word)) { outF << compWord << endl; inF >> compWord; } if (compWord != word) { cout << word << " not found in the dictionary. \n"; cout << " Do you want to insert " << word << " into the dictionary? "; int res = getch(); // un-buffered input cout << endl; if ((char)res == 'y' || (char)res == 'Y') { cout << " inserting " << word << " into the dictionary ...\n"; outF << word << endl; changed = 1; } } while (inF) { outF << compWord << endl; inF >> compWord; } inF.close(); outF.close(); // update the old dictionary if (changed == 1) { cout << "do you want to rename the updated dictionary? "; int res = getch(); cout << endl; if ((char)res == 'Y' || (char)res == 'y') { cout << " renaming files ...\n"; rename("words.tmp","words"); } } }