Question

I have a little problem,

I've got a file containing a lot of information ( posted on pastbin, because its pretty long, http://pastebin.com/MPcTMHfd )

Yes, it is a PokerStars card log, but I am making a odd calculator for myself, I even asked PokerStars about that.

When I filter the file I get something like

CODE FOR FILTERING:

 getline(failas,line);
    if( line.find(search_str) != std::string::npos )
        {
            firstCard = line.substr(4);
            cout << firstCard << '\n' ;
        }

- Result:

::: 7c 
::: 5d 
::: 13c
::: 7d
::: 12h
::: 13d

and so on, so the thing I want to do is get last cards ( 12h and 13h as in the example above )

All I managed to get is last card, (13d) Any ideas, how I could read two lines or any other ideas how to solve my little problem?

I know that it is a beginner question, but I haven't really found a suitable answer anywhere.

Was it helpful?

Solution

So you want the n last cards ? Then maintain a "last n cards" list and update it for every card found.

Like that : (code written by hand)

#include <list>
#include <string>

std::list<std::string> last_n_cards;
const unsigned int last_n_cards_max = 2;

// basic init to make code simpler, can be done differently
last_n_cards.push_back( "?" ); // too lazy to write the for(last_n_cards_max) loop ;)
last_n_cards.push_back( "?" );

(loop)
  if( line.find(search_str) != std::string::npos )
  {
    currentCard = line.substr(4);
    cout << currentCard << '\n';

    last_n_cards.push_back(currentCard); // at new card to the end
    last_n_cards.pop_front(); // remove 1 card from the front
    }

// At the end :
cout << "Last two cards are : " << last_n_cards[0] << " and " << last_n_cards[1] << '\n';

std::list API is here http://en.cppreference.com/w/cpp/container/list

Note : have you considered using another language than C++ for this task ? Non perf-intensive files parsing may be easier with a dynamic language like python.

OTHER TIPS

There could be two simple methods to solve your problem.

  1. Scan whole file to figure out how many lines (cards) it has and then simply set position to one before last card and read it and then the next. You will have two last cards read.

  2. Use two pointers. Set first pointer to first line and next one to first + 2. Then iterate through whole file and once second pointer will reach end of file (EOF) your first pointer will point to one before last card.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top