What is a better way to get C++ get text data into a program rather than using fstream

  c++, database, file, fstream, txt

this is a very challenging question as I’m not sure how to ask it correctly.

I wrote a program that will read in the same text data on load every time. The text data is a list of words in the dictionary and the program solves anagram type puzzles (like Jumble, Boggle, Scrabble, etc). This text data never changes. Currently I use to read a text file which MUST be present in the same folder as the .exe that is built. This assumes the user would not just go in and erase, edit, or otherwise corrupt the text file or something really within the realm of possibility for a user to do. Not only that but locking a file and reading it are very noticeably slow operations.

Most of what the program does is convert the .txt file into an abstract data type (ADT) which sorts the word into a ‘signature’ then builds a set of words that have the same signature. These sets are stored in a structure (called map here) that is a key:value type data structure where the key is the signature. Anyway that info is irrelevant to the question just sufficient to say that on load I need to build my ADT in memory. I’m looking for a more sophisticated way than text files.

Since I’m new at programming there is probably a much better way. I just don’t know how to even ask the question since I don’t know what is available out there.

I understand databases, but then again it seems like that relies on an external file. I have seen posts which talk about storing data in a .h file but they always want to build a character array (char[i]), which would require converting this data into my ADT and that seems again like a waste of time when the program is loading. (why bother converting it into a char array just to read it back to the ADT?)

    /*
     * Project: myJumble
     * Created by CS106 C++ Assignment Wizard 0.1
     *
     * Name: Brad Beall
     * Section: Life
     * This code will solve the jumble puzzles in the newspaper.
     */
    
    #include <fstream>
    #include <iostream>
    #include "simpio.h"
    #include "map.h"
    #include "set.h"
    #include "genlib.h"
    
    //This function swaps two characters. 
    void Swap(char &ch1, char &ch2)
    {
        char tmp = ch1;
        ch1 = ch2;
        ch2 = tmp;
    }
    
    //This function sorts all the chars in a word in alphabetical order
    string SortWord(string inWord)
    {
        
        inWord = ConvertToLowerCase(inWord);
        //these two for loops will sort the string alphabetically
        // - idea is starting from the front, find the 'smallest' character in the string.
        //   (where a is 'smaller' than b)
        //   then move that smallest character to the front of the string
        //   now move to the next character and again look for the smallest character.
        //   Example: for "peach", first move the 'a' to the front to form "apech", then move 'c' to form "acpeh"...
        for (int i = 0; i < inWord.length(); i++) {
            int minIndex = i;
            for (int j = i+1; j < inWord.length(); j++)
            {
                if (inWord[j] < inWord[minIndex])
                {
                    // looking for the 'smallest' character
                    minIndex = j;
                }
            }
            Swap(inWord[i], inWord[minIndex]);
        }
    
        return inWord;
    }
    
    void BuildDictionary(Map<Set<string> > &kDict, ifstream &in)
    {
        string nextWord = "";
        while(true)
        {
            //read in the next word from the dictionary
            in >> nextWord;
            if (in.fail()) break;
            //sort letters alphabetically using SortWord, use that as the key 
            // and then add that key:value pair to the set.
            kDict[SortWord(nextWord)].add(nextWord);
        }
    }
    
    //this function prints a set
    void PrintSet(Set<string> &inputSet)
    {
        Set<string>::Iterator it = inputSet.iterator();
        while (it.hasNext())
        {
            cout << it.next() << endl;
        }
    }
    
    int main ()
    {
        ////debug the function: string SortWord(string inWord)
        //cout << "Enter a word to sort" << endl;
        //string tempString = GetLine();
        //tempString = SortWord(tempString);
        //cout << tempString;
    
        //building the dictionary may take some time.
        cout << "Loading the dictionary.  This may take some time." << endl;
    
        //read in the text file with all dictionary words
        ifstream in;
        in.open("enable1.txt");
        //call the member function that will create our data structure
        //this will be a MAP:
        // - key: the alphabetized letters from a word, or the word's "signature"
        // - value: a Vector of words with the matching signature
        Map<Set<string> > keyedDictionary;
        BuildDictionary(keyedDictionary, in);
    
        while(true)
        {
            //prompt user for a word to solve
            cout << "Enter a jumbled word to solve." << endl;
            cout << "Type '0' to exit." << endl << endl;
    
            string solveWord = GetLine();
            if(solveWord == "0"){
                break;
            }
    
            //sort the word into a signature key
            solveWord = SortWord(solveWord);
            //call the PrintSet(Set) member function to print the set of solutions for this signature key
            PrintSet(keyedDictionary[solveWord]);
        }
    
    
        return 0;
    }

Source: Windows Questions C++

LEAVE A COMMENT