Parsing and tracking unknown variable names

This is a discussion on Parsing and tracking unknown variable names within the C++ Programming forums, part of the General Programming Boards category; I am going to be parsing a log file from a game to determin how many times a person casted ...

  1. #1
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094

    Parsing and tracking unknown variable names

    I am going to be parsing a log file from a game to determin how many times a person casted the same spell. I don't know though, how to store data on a thing without first making a variable for it. I have this so far:

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    inline void loadfile(ifstream& file)
    {
        file.open("eqlog_Brahman_povar.txt");
        int attempt = 1;
        while(attempt==1)
        {
            if (!file.is_open())
            {
                string filename;
                cout<<"Please specify the name, and possibly"<<endl
                    <<"the path to the EQ log."<<endl;
                getline(cin,filename);
                file.open(filename.c_str());
            }
            if (!file.is_open())
            {
                cout<<"The file does not appear to be there!"<<endl
                    <<"If you wish to try again, please press enter 1."<<endl
                    <<"Press any other key to exit the program and give up.";
                cin>>attempt;
                    
            }
            else
            {
                cout<<"The file is open!";
                attempt = 0;
            }
        }
    }
    
    inline void counter(ifstream& file)
    {
        int datestamp = 27;
        int count = 0;
        string line;
        while (getline( file , line ))
    	{
            int read_pos = line.find("begins",0);
            int spell_pos = line.find("<",0);
            if(read_pos>0 && spell_pos>0)
            {
                string caster = line.substr(datestamp,read_pos-datestamp);
                cout<<"Caster:"<<caster;
                string spell = line.substr(spell_pos+1,line.size()-spell_pos-2);
                cout<<" Spell: "<< spell<<endl;
                count++;
            }
        }
        cout<<"Spells casted: "<<count;
    }            
    
    int main()
    {
        ifstream file;
        loadfile(file);
        counter(file);
        cin.get();
    }
    I know inline voids aren't most peoples favorite, but that is what I used for this first version, later versions will probably need complete rewrites to make it faster anyway.
    ~Wraith

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    you could use std::map that associates a string (spell name) with an int (number of times the spell was cast). I don't know map well enough to tell you how to use it. I'm sure if you searched this message board or goodled around you will find out how to do it.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21