// Source: "Software Design ...", John A Robinson, Newnes, 2004, page 94. #include // Required for file i/o #include using namespace std; int main(int argc, char **argv) { if (argc != 2) { cout << "Usage: readlocs locationsfile\n"; return -1; } ifstream infile(argv[1]); // Declares a stream object, and makes it // refer to the input file (the first and // only argument on the program command line) if (!infile) // If the file could not be found, infile is 0 { cout << "Can't open " << argv[1] << " for reading\n"; return -1; } int numrecords; // First line of database file is number of // records, so... infile >> numrecords; // Using getfrom operator - just like // standard input! for (int i = 0; i < numrecords; i++) { char name[20]; float latitude, longitude; char ns, ew; // Get records one by one and print them out // on the screen infile >> name >> latitude >> ns >> longitude >> ew; cout << "Location " << name << " is at latitude " << latitude << ' ' << ns << ", longitude " << longitude << ' ' << ew << '\n'; } infile.close(); return 1; }