辞書ファイルをバイナリ検索ツリーに読み込もうとしています。私はそれが単語を形成するまで、その単語に定義を付けるまで、各ノードに文字の組み合わせをロードしたいと思います。例:txtファイルで読むC++
C:
Ca:
Cat: a mammal. Closely related to a Liger.
現在、私はサンプルファイルを読み込もうとしていますが、私はinFile.fail()条件を受け取り続けています。どんな助け、アドバイス、コードレビューも大変ありがとうございます。
bool Dictionary::insertTrie(string word, string def)
{
Node* newNode = createTrie();
string tempW;
bool retVar;
bool found = searchNode(root, word);
if(found)
retVar = false;
while(!found){
newNode->word = word;
newNode->definition = def;
insert(root, newNode);
retVar = true;
}
/*while(!found){
for(int i = 0; i < word.length(); i++){ //loop to iterate through the string
for(int j = 0; j < ALPHABET_SIZE; j++){ //loop to iterate the nodes
tempW += word[i];
newNode->word = word;
newNode->definition = def;
newNode = newNode->next[j];
}
retVar = true;
}*/
return retVar;
}
bool Dictionary::loadDictionary(string fileName)
{
fstream inFile;
string file;
string words;
string defs;
string tempW;
bool retVar;
inFile.open(fileName, ios::in); // opens
cout << "\nLoading Dictionary...\n";
if(inFile.fail()){
retVar = false;
cout << "ERROR: Dictionary file failed to load. Please try again.\n";
}
else{
while(!inFile.eof()){ //loop reads in words and definitions until the end of file bit is received
for(int i = 0; i < words.length(); i++){
getline(inFile, words, ':'); //receives input of the words stopping at the ':' delimiter
tempW += words[i];
getline(inFile, defs, '\n'); //receives input of the defs stopping at the '\n' delimiter
insertTrie(tempW, defs); //inserts the words and the definition
}
}
retVar = true;
}
inFile.close(); //closes the file
return retVar;
}
こんにちは!私はこれがかなりのコードであることを認めなければなりません。これは本当に**本当に**最小の**不正確な例ですか?私はすべての "入力処理"ロジックは機能をテストするために全く必要ではないと思います。エラーを再現するのに必要な最小限にあなたの例を減らしてください。 –