#include #include #include #include using namespace std; /* f.open(string name); f.open(string name, ios::in | ios::out | ios::binary | ios::trunc | ios::app | ios::ate); f.write(char * pointer, int numbytes); f.read(char * pointer, int numbytes); int bytesread = f.gcount(); int f.tellg(); // reading position int f.tellp(); // writing position f.seekg(int relative_position, ios::beg or ios::end or ios::cur); f.seekp(int relative_position, ios::beg or ios::end or ios::cur); */ int main() { const string fname = "/home/www/class/een318/named-places.txt"; ifstream fi(fname); if (fi.fail()) { cerr << "Can't read \"" << fname << "\"\n"; exit(1); } fi.seekg(0, ios::end); int filesize = fi.tellg(); fi.seekg(0, ios::beg); int count = 0; char line[120]; int minpos = 0, maxpos = filesize / 115 - 0; while (minpos <= maxpos) { int midpos = (minpos + maxpos) / 2; fi.seekg(midpos * 115, ios::beg); fi.read(line, 115); if (fi.gcount() != 115) break; line[115] = 0; char c = line[58]; line[58] = 0; cout << line + 10 << "\n"; if (string(line + 10) == "New York ") { line[58] = c; cout << "line " << count << ": " << line; break; } else if (string(line + 10) < "New York ") minpos = midpos + 1; else maxpos = midpos - 1; } cout << "Total " << count << " lines\n"; cout << "File size " << filesize << ", " << count << " * 115 = " << count * 115 << "\n"; fi.close(); }