[SOLVED] Parsing a text file

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
lotios611
Chaos Rift Regular
Chaos Rift Regular
Posts: 160
Joined: Sun Jun 14, 2009 12:05 pm
Current Project: Game engine for the PC, PSP, and maybe more.
Favorite Gaming Platforms: Gameboy Micro
Programming Language of Choice: C++

[SOLVED] Parsing a text file

Post 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";
    }
Last edited by lotios611 on Tue Jan 26, 2010 6:56 pm, edited 1 time in total.
"Why geeks like computers: unzip, strip, touch, finger, grep, mount, fsck, more, yes, fsck, fsck, fsck, umount, sleep." - Unknown
User avatar
short
ES Beta Backer
ES Beta Backer
Posts: 548
Joined: Thu Apr 30, 2009 2:22 am
Current Project: c++, c
Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
Programming Language of Choice: c, c++
Location: Oregon, US

Re: Parsing a text file

Post 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?
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
User avatar
lotios611
Chaos Rift Regular
Chaos Rift Regular
Posts: 160
Joined: Sun Jun 14, 2009 12:05 pm
Current Project: Game engine for the PC, PSP, and maybe more.
Favorite Gaming Platforms: Gameboy Micro
Programming Language of Choice: C++

Re: Parsing a text file

Post 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.
"Why geeks like computers: unzip, strip, touch, finger, grep, mount, fsck, more, yes, fsck, fsck, fsck, umount, sleep." - Unknown
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: Parsing a text file

Post 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?
User avatar
lotios611
Chaos Rift Regular
Chaos Rift Regular
Posts: 160
Joined: Sun Jun 14, 2009 12:05 pm
Current Project: Game engine for the PC, PSP, and maybe more.
Favorite Gaming Platforms: Gameboy Micro
Programming Language of Choice: C++

Re: Parsing a text file

Post 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.
"Why geeks like computers: unzip, strip, touch, finger, grep, mount, fsck, more, yes, fsck, fsck, fsck, umount, sleep." - Unknown
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Parsing a text file

Post 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. ;)
Last edited by dandymcgee on Thu Jan 07, 2010 8:14 pm, edited 1 time in total.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
lotios611
Chaos Rift Regular
Chaos Rift Regular
Posts: 160
Joined: Sun Jun 14, 2009 12:05 pm
Current Project: Game engine for the PC, PSP, and maybe more.
Favorite Gaming Platforms: Gameboy Micro
Programming Language of Choice: C++

Re: Parsing a text file

Post by lotios611 »

Thanks for that code man! I have one question though. What does "file.ignore(3)" do?
"Why geeks like computers: unzip, strip, touch, finger, grep, mount, fsck, more, yes, fsck, fsck, fsck, umount, sleep." - Unknown
User avatar
lotios611
Chaos Rift Regular
Chaos Rift Regular
Posts: 160
Joined: Sun Jun 14, 2009 12:05 pm
Current Project: Game engine for the PC, PSP, and maybe more.
Favorite Gaming Platforms: Gameboy Micro
Programming Language of Choice: C++

Re: Parsing a text file

Post 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?
"Why geeks like computers: unzip, strip, touch, finger, grep, mount, fsck, more, yes, fsck, fsck, fsck, umount, sleep." - Unknown
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Parsing a text file

Post 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. ;)
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
lotios611
Chaos Rift Regular
Chaos Rift Regular
Posts: 160
Joined: Sun Jun 14, 2009 12:05 pm
Current Project: Game engine for the PC, PSP, and maybe more.
Favorite Gaming Platforms: Gameboy Micro
Programming Language of Choice: C++

Re: Parsing a text file

Post 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.
"Why geeks like computers: unzip, strip, touch, finger, grep, mount, fsck, more, yes, fsck, fsck, fsck, umount, sleep." - Unknown
User avatar
lotios611
Chaos Rift Regular
Chaos Rift Regular
Posts: 160
Joined: Sun Jun 14, 2009 12:05 pm
Current Project: Game engine for the PC, PSP, and maybe more.
Favorite Gaming Platforms: Gameboy Micro
Programming Language of Choice: C++

Re: Parsing a text file

Post 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.
"Why geeks like computers: unzip, strip, touch, finger, grep, mount, fsck, more, yes, fsck, fsck, fsck, umount, sleep." - Unknown
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Parsing a text file

Post 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 == "").
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
lotios611
Chaos Rift Regular
Chaos Rift Regular
Posts: 160
Joined: Sun Jun 14, 2009 12:05 pm
Current Project: Game engine for the PC, PSP, and maybe more.
Favorite Gaming Platforms: Gameboy Micro
Programming Language of Choice: C++

Re: Parsing a text file

Post by lotios611 »

Thanks! This is like the only project I've ever finished.
"Why geeks like computers: unzip, strip, touch, finger, grep, mount, fsck, more, yes, fsck, fsck, fsck, umount, sleep." - Unknown
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Parsing a text file

Post by dandymcgee »

lotios611 wrote:Thanks! This is like the only project I've ever finished.
Sure thing. Congrats on finishing.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
Post Reply