Page 1 of 1

[SOLVED] Parsing a text file

Posted: Tue Jan 05, 2010 6:06 pm
by lotios611
I'm trying to parse text files into an std::map. The text file is set up like this:
Hello - A word
Testing - Testing
APPLES - ANOTHER WORD
banana - a fruit
Here's some code I have written so far, it's not much though.

Code: Select all

    std::fstream file;
    std::string line;
    file.open("File.txt");
    if (file.is_open())
    {
        while (!file.eof())
        {
            while (std::getline(file, line))
            {
                std::string temp;
                std::istringstream isstream(line);
                int i = 0;
                while (std::getline(isstream, temp, '-'))
                {
                    testing[temp] = "";
                    std::cout << i << temp << " ";
                    i++;
                }
                std::cout << "\n";
            }
        }
        file.close();
    }
    else
    {
        std::cout << "Unable to load file";
    }

Re: Parsing a text file

Posted: Tue Jan 05, 2010 9:54 pm
by short
So..........................do you have a question...................... do you want someone to look at your code......................... is there an error? or are you simply sharing how you did it?

Re: Parsing a text file

Posted: Wed Jan 06, 2010 5:21 am
by lotios611
I can't believe I left that part out. What I'm trying to do is separate the words and store them into an std::map.

Re: Parsing a text file

Posted: Wed Jan 06, 2010 8:42 am
by XianForce
lotios611 wrote:I can't believe I left that part out. What I'm trying to do is separate the words and store them into an std::map.
And the problem is?

Re: Parsing a text file

Posted: Wed Jan 06, 2010 6:29 pm
by lotios611
I should really be more specific. My problem is that I have no idea how to separate the two words and put them into an std::map. I want only the words stored in the std::map.

Re: Parsing a text file

Posted: Wed Jan 06, 2010 9:06 pm
by dandymcgee

Code: Select all

#include <iostream>
#include <string>
#include <map>
#include <fstream>

int main()
{
    std::ifstream file( "config.txt" );
    std::map<std::string, std::string> dictionary;
    std::map<std::string, std::string>::iterator iter;

    if (file.is_open())
    {
        while (!file.eof())
        {
            //Get word
            std::string word;
            file >> word;

            //Ignore the " - "
            file.ignore( 3 );

            //Get definition and store in dictionary map (max definition size 100 characters can be changed)
            char def[100];
            file.getline(def, 100);
            dictionary[word] = def;
        }
        file.close();
    }
    else
    {
        std::cout << "Unable to load file";
    }

    //Iterating through dictionary in its entirety
    for( iter = dictionary.begin(); iter != dictionary.end(); iter++ )
    {
        std::cout << "Word: " << iter->first << "\n"
                  << "Definition: " << iter->second << "\n\n";
    }

    //Looking up words
    std::cout << "Definition of Monkey is '" << dictionary["Monkey"] << "'.\n";

    std::cin.get();

    return 0;
}
Sample config file [config.txt]:

Code: Select all

Monkey - Crazy animal
Apple - Red delicious fruit
Programming - How smart people have fun
Keep in mind words are stored case sensitive, you may want to convert them all to lowercase before entering them into the map.

EDIT: Decided to put some effort into it to give you working code this time. ;)

Re: Parsing a text file

Posted: Thu Jan 07, 2010 6:32 pm
by lotios611
Thanks for that code man! I have one question though. What does "file.ignore(3)" do?

Re: Parsing a text file

Posted: Thu Jan 07, 2010 6:36 pm
by lotios611
I think I figured it out. First, read in the file until before the first space. Then ignore the junk. Finally, put the word and the definition into the dictionary. Right?

Re: Parsing a text file

Posted: Thu Jan 07, 2010 8:13 pm
by dandymcgee
lotios611 wrote:I think I figured it out. First, read in the file until before the first space. Then ignore the junk. Finally, put the word and the definition into the dictionary. Right?
Yup. file.ignore(3) ignores 3 characters which always happen to be the " - " between the word and the definition. ;)

Re: Parsing a text file

Posted: Sat Jan 16, 2010 8:39 am
by lotios611
Alright, I'm still having troubles. If I run the program more then once, the first element in the map is a space.

Re: Parsing a text file

Posted: Mon Jan 25, 2010 12:12 pm
by lotios611
I'm almost done this project. I only have one problem. If I run the program, add a word, then close the program, run it again, then print all of the words, one of the words is a space.

Re: Parsing a text file

Posted: Mon Jan 25, 2010 12:20 pm
by dandymcgee
lotios611 wrote:I'm almost done this project. I only have one problem. If I run the program, add a word, then close the program, run it again, then print all of the words, one of the words is a space.
Don't put an empty line at the end of your dictionary file. Either that or remember to ignore it (check if word == "").

Re: Parsing a text file

Posted: Tue Jan 26, 2010 6:55 pm
by lotios611
Thanks! This is like the only project I've ever finished.

Re: Parsing a text file

Posted: Tue Jan 26, 2010 7:50 pm
by dandymcgee
lotios611 wrote:Thanks! This is like the only project I've ever finished.
Sure thing. Congrats on finishing.