简体   繁体   中英

Reading in txt file c++

I'm attempting to load a dictionary file into a binary search tree. I would like to load each node with a combination of letters until it forms a word, thus attaching a definition to that word. Example:

C:
Ca:
Cat: a mammal. Closely related to a Liger.

Currently, I'm attempting to load the sample file and I keep receiving my inFile.fail() conditions. Any help, advice, and/or code-review is greatly appreciated.

Here are the two functions which I think may be causing my issue:

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;
}
     while(!inFile.eof()){

This is always a bug .

            getline(inFile, words, ':');  //receives input of the words stopping at the ':' delimiter

And if the input line does not contain a semicolon, this will swallow the entire line, and start reading the next line. And the next line. As long as it takes to find a semicolon. This is obviously wrong.

You need to make two changes here.

  • Check for end of file, properly

  • Use std::getline() to read a single line of text into a std::string , and after the whole line is read, check if the read std::string contains a semicolon.

Furthermore, you asked "I'm attempting to load the sample file I keep receiving inFile.fail() conditions". If so, then posting hundreds of lines of code that had absolutely nothing to do, whatsoever, with reading from the file, served absolutely no purpose, and will likely earn you a few downvotes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM