1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cstdlib>
using namespace std;
struct Card {
int card_id;
int book_id;
string name;
string phone_number;
};
string next_token(const string &line, const char tok, size_t &beg, size_t &pos) {
string res("");
pos = line.find(tok, beg);
if (string::npos != pos)
{
res = line.substr(beg, pos - beg);
beg = pos + 1;
}
string white_space(" \t\n\r\f\v");
res.erase(0, res.find_first_not_of(white_space));
res.erase(res.find_last_not_of(white_space) + 1);
return res;
}
void print_card(Card *card) {
cerr << endl << card->name << endl;
cerr << card->phone_number << endl;
cerr << card->card_id << endl;
cerr << card->book_id << endl << endl;
}
int main()
{
vector<Card *> cards;
ifstream f_cards("Cards.txt");
char tok = ',';
string line;
while(getline(f_cards, line)) {
Card *card = new Card;
size_t beg_pos = 0;
size_t pos = 0;
card->name = next_token(line, tok, beg_pos, pos);
card->phone_number = next_token(line, tok, beg_pos, pos);
card->card_id = strtol(next_token(line, tok, beg_pos, pos).c_str(), 0, 10);
card->book_id = strtol(next_token(line, tok, beg_pos, pos).c_str(), 0, 10);
cards.push_back(card);
}
for (int i = 0; i < cards.size(); ++i) {
print_card(cards[i]);
}
for (vector<Card *>::reverse_iterator ri = cards.rbegin(); ri != cards.rend(); ++ri)
{
delete *ri;
}
cards.clear();
return 0;
}
|