#include #include #include #include using namespace std; int main() { ifstream fin; fin.open("/home/www/dics/web2.txt"); if (fin.fail()) { cerr << "could not open dictionmary file\n"; exit(1); } vector positions; int pos = 0; while (true) { string line; getline(fin, line); if (fin.fail()) break; if (line[0] >= 'a' && line[0] <= 'z') positions.push_back(pos); pos += line.length() + 1; } fin.clear(); cout << "search for: "; string wanted; cin >> wanted; int first = 0, last = positions.size()-1; bool found = false; while (first <= last) { int mid = (first + last)/2; fin.seekg(positions[mid]); string line; getline(fin, line); cout << mid << ": " << line << "\n"; if (wanted == line) { cout << "word found at position " << mid << "\n"; found = true; break; } if (wanted < line) last = mid -1; else first = mid + 1; } if (! found) cout << "word not present\n"; fin.close(); }