Code:
if ((fp = fopen(file, "r")) == NULL) { perror("opening the file"); exit(1); }
Solution:
Of course the solution here is the same as in the previous problem, namely:
if ((fp = fopen(file, "r")) == NULL) { perror(file); exit(1); }
The only defect in the original code this time is the way in which perror() is used. The argument to perror() should be a file name if one is available. The original code will generate a message like this (assuming the "file" variable contains the value "hello.c"):
opening the file: Permission deniedwhereas the revised version will look like:
hello.c: Permission deniedThe latter is a standard error message, and is more informative.
Don't underestimate the value of standardization. If every unix utility output error messages in a different format, unix would be a lot harder to become expert in.
And don't attempt creativity in this realm. There are many ways to be creative in computer programming which are more exciting than producing substandard error messages.