#include #include #include using namespace std; typedef unsigned char byte; struct obs { short int year; byte month, day; short int mint, avgt, maxt, prec; }; int main() { if (sizeof(obs) != 12) { cerr << "Sizeof obs unexpected value " << sizeof(obs) << "\n"; exit(1); } ifstream fin("obs.dat", ios::in | ios::binary); if (fin.fail()) { cerr << "file trouble\n"; exit(1); } int year, month, day, ignore; double mint, avgt, maxt, prec; fin.seekg(0, ios::end); // to find size of file move to end and ask position int lastrecord = fin.tellg() / sizeof(obs) - 1; while (true) { cout << "Y M D: "; int y, m, d; cin >> y >> m >> d; int wanted = (y * 10000) + (m * 100) + d; int first = 0, last = lastrecord; while (true) { if (first > last) { cout << "not found\n"; break; } int mid = (first + last) / 2; fin.seekg(mid * sizeof(obs), ios::beg); obs o; fin.read((char *)& o, sizeof(o)); int found = (o.year * 10000) + (o.month * 100) + o.day; if (wanted == found) { cout << o.year << " " << (int)o.month << " " << (int)o.day << " " << (double)o.mint/10 << " " << (double)o.avgt/10 << " " << (double)o.maxt/10 << " " << (double)o.prec/10 << "\n"; break; } if (wanted < found) last = mid - 1; else first = mid + 1; } } fin.close(); }