The C-string solution for the Rolodex problem is logically identical to string class solution presented in the previous section, differing only in the kind of strings and getline function used. For brevity, the example omits the initial development.
#include <iostream> #include <fstream> #include <iomanip> #include <cstring> using namespace std; int main() { ifstream in("rolodex.txt"); if (!in.good()) { cerr << "Unable to open \"rolodex.txt\"\n"; exit(1); } /*char line[100]; while (in.getline(line, 100)) cout << line << endl;*/ while (!in.eof()) // (a) { char name[20]; // (b) in.getline(name, 20, ':'); // (c) char address[35]; // (b) in.getline(address, 35, ':'); // (c) char phone[20]; // (b) in.getline(phone, 20, '\n'); // (c) //in.getline(phone, 20); // (d) cout << left << setw(20) << name << setw(35) << address << setw(20) << phone << endl; } return 0; }
View | Download | Comments |
---|---|---|
c-rolodex.cpp | c-rolodex.cpp | An example program reading and parsing fields from a line-oriented data file, saving them in C-strings for processing. |