0

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.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Ufikas
  • 22
  • 1
  • 6
  • 1
    I'm going to assume there is a helluva lot more code than that. Post the *entire read-loop* as well as your *real* input file and the *related* output file, both desired *and* currently exhibited form. Do that and you *may* get some answers, but even then its questionable. – WhozCraig Dec 10 '13 at 09:39
  • Do you mean that your firstCard has a value 13d and you want some other variable to store 12h as well? Then you can use two variables and alternately update values in them. – Abhishek Bansal Dec 10 '13 at 09:40
  • Just give some similar link on the web: http://stackoverflow.com/questions/11876290/c-fastest-way-to-read-only-last-line-of-text-file http://stackoverflow.com/questions/17877025/reading-last-n-lines-from-file-in-c-c – vivi Dec 10 '13 at 12:01

2 Answers2

1

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.

Offirmo
  • 18,962
  • 12
  • 76
  • 97
  • Thank you, dear sir! :) Never knew about lists (DAMN YOU SCHOOL!), thats a pretty nice feature. Thanks again! And I like C++ syntax more tho, but i guess i cant give it a shot – Ufikas Dec 10 '13 at 11:08
  • @Ufonautas hem you **like C++ syntax more** ? I'm a C++ professional, I like C++ too but I know to use it for the right job. C++ typed and dated interface is a pain. For such a parsing task, I would go straight to python (or even javascript with node.js). If you don't know at last one dynamic language, I humbly suggest you to learn one of them, as they complement C++ nicely. – Offirmo Dec 12 '13 at 16:49
0

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.

codewarrior
  • 1,269
  • 1
  • 9
  • 14