0

I am trying to read in a file formated like this:

"Champion: "aatrox
  Aatrox heals for 20 / 22.5 / 25 / 27.5 / 30% of the premitigation-physical damage he deals, with a 15% effectiveness against non-champions.
  Additionally, Aatrox stores a Umbral Dash charge every 24 / 20 / 16 / 12 / 8 seconds, up to 2 stored at once.
  Aatrox dashes in the target direction, gaining 15 / 25 / 35 / 45 / 55 bonus attack damage for 1.5 seconds.
  Umbral Dash can be cast during his other spellcasts without interrupting them.
"the end"
"Champion: "ahri
  Ahri blows a kiss that deals 60 / 90 / 120 / 150 / 180 (+40% of ability power) magic damage to an enemy and charms it, causing them to walk harmlessly towards her for 1.4 / 1.55 / 1.7 / 1.85 / 2 seconds.
  When Charm damages a champion, Ahri's abilities deal 20% more damage to them for 5 seconds.
"the end"

I need to be able to read in the champion name, store it in a string, then read in the information after and store it in a string

  • 1
    Not clear on what the problem is. Having trouble writing the parser? Having trouble with [`std::unordered_map`](https://en.cppreference.com/w/cpp/container/unordered_map)? If both, start by writing the parser and worry about storage later. Easier to solve one job at a time than two. – user4581301 Dec 06 '18 at 22:13
  • writing the parser – Savage Potato Dec 06 '18 at 22:14
  • Which line of input describes `qAbility`. Heck, what the heck is a `qAbility`? Variable names are of limited value if they don't describe what they contain. Might as well just call this one `q` and save on the typing since it doesn't contain much extra value. – user4581301 Dec 06 '18 at 22:16
  • Sharing your research helps everyone. Tell us what you've tried and why it didn’t meet your needs. This demonstrates that you’ve taken the time to try to help yourself, it saves us from reiterating obvious answers, and most of all it helps you get a more specific and relevant answer! See also: [ask] – Sean Pianka Dec 06 '18 at 22:37

1 Answers1

0

I prefer regex because its really flexible. Also if you use boost you can directly run regex through file as claimed from Using a regex_iterator on an istream

below is sample code you can easily implement

#include <iostream>
#include <fstream>
#include <regex>

int main(int argc, char **argv) {
    const std::regex characterPattern(R"-(("Champion:\s*")(\w+))-");

    std::ifstream charsfile;
    charsfile.exceptions (std::ifstream::failbit | std::ifstream::badbit );
    charsfile.open("/home/abdurrahim/projects/testregex/test.txt");

    const std::string charstext((std::istreambuf_iterator<char>(charsfile)),
                 std::istreambuf_iterator<char>());

    charsfile.close();

    for(std::sregex_iterator i(charstext.begin(), charstext.end(), characterPattern); i != std::sregex_iterator(); ++i)
    {
        std::smatch m = *i;

        std::cout << m[1] << " found at " << m.position() << " with name '"  << m[2] << "'" << std::endl;
    }

    return 0;
}

I use online regex tester to play around finding such regex patterns

Abdurrahim
  • 2,078
  • 1
  • 17
  • 23