Heckendorn’s Law is based on a saying of the great Robert Heckendorn:
That is, don’t do this:
ifstream in(pwfile); if (!in) { cerr << "Bad file\n"; return 1; }
Imagine the frustration of the user who reads this uninformative message. Which file is bad? What was wrong with it? What was the program trying to do with the file? What program was running, anyway?
There’s always more information that you can provide:
argv[0]
Here’s how to do it:
#include <iostream> #include <fstream> // for ifstream #include <cstdio> // for perror using namespace std; int main(int /* argc */, char *argv[]) { const char pwfile[] = "/etc/shadow"; // Why not a std::string? ifstream in(pwfile); if (!in) { cerr << argv[0] << ": can’t read shadow password file "; perror(pwfile); return 1; } return 0; }
Modified: 2014-02-02T10:17 User: Guest Check: HTML CSSEdit History Source |
Apply to CSU |
Contact CSU |
Disclaimer |
Equal Opportunity Colorado State University, Fort Collins, CO 80523 USA © 2015 Colorado State University |