aa.cpp

(plain text)
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
#include <algorithm>
#include <fstream>
#include <iostream>
#include <iterator>
#include <string>
using namespace std;

int main (int argc, char *argv[]) {
  ifstream ifs("aa.txt");
  string s = "AAA";
  istreambuf_iterator<char> pos(ifs);
	
  for (int i = 0; i < 4; ++i) {
    pos = search(pos, istreambuf_iterator<char>(), 
		 s.begin(), s.end());		
  }

  if (ifs) {
    // place cursor at beginning of fourth instance
    ifs.seekg(1 - s.size(), ios::cur);

    // output the position of fourth instance
    cout << ifs.tellg() << endl;
  }

  return 0;
}