how to remove code duplication

C

Chris Torek

... The error messages will [with the above line of code] include
the pathname of the file that caused the error, and that information
can be hugely helpful to understanding a problem. "FOPEN: permission
denied" is much less informative than "/etc/passwd: permission
denied".[/QUOTE]

I prefer the even-more-detailed form:

progname: cannot open /etc/passwd for writing: Permission denied.

which one can obtain with, e.g.:

if ((fp = fopen(filepath, "w")) == NULL) {
fprintf(stderr, "%s: cannot open %s for %s: %s\n",
progname, filepath, "writing", strerror(errno));
... additional action here as needed ...
}
It is a design choice; it does require extra work to keep
track of the path names in order to report them, and I am suggesting
that it is worth the effort.

The above version requires even more work, of course: you must
also record the program-name somewhere. (If the open-or-fail
routine is a subroutine, it is best not to hard-code the program
name, so that the subroutine is reuseable.)

If you wish to protect against implementations that fail to set
errno in fopen, you can even do things like:

FILE *open_file_or_die(const char *filepath, const char *mode) {
FILE *fp;

errno = 0;
if ((fp = fopen(filepath, mode)) == NULL) {
char *mode_as_verb = (*mode == 'r') ? "reading" : "writing";
char *err = errno ? strerror(errno) : "no system error reported";

fprintf(stderr, "%s: cannot open %s for %s: %s\n",
progname, filepath, mode_as_verb, err);
exit(EXIT_FAILURE); /* or abort(), perhaps */
}
return fp;
}

and/or you can use text like "last system error reported" to
mark the string returned by strerror(errno).

(It is tempting to make the "verb" be "read" or "writ" and put
the "ing" into the format string. This works well enough in
English, but if you ever decide to have the error messages
get translated into other languages, maybe not so well anymore.)
 
A

Antoninus Twink

Well, C90 doesn't support the for(int i... syntax. It was new in C99. So if
you want to use it, you have to give up C90 conformance, which in turn
means restricting the ability of comp.lang.c people to help you (insofar
as people who don't have C99 compilers won't be able to compile your
stuff, which clearly limits their scope in replying to you).

Just when will you give over with these lies and nonsense?

You yourself Richard Heathfield have said you use gcc, which will
compile for(int i...) syntax just fine, even though by your idiotic
definition it "isn't a C99 compiler".
 
T

Tim Rentsch

santosh said:
[...]

Virtually every programming editor I have used renders code between #if
0 #endif in the same colour as code between /* and */ or // and EOL.

If virtually every one does, does that mean some of them don't?

:)
 
N

Nick Keighley

santosh said:
Virtually every programming editor I have used renders code between #if
0 #endif in the same colour as code between /* and */ or // and EOL.

If virtually every one does, does that mean some of them don't?

:)

they're usually configuable.

ConTEXT doesn't do this. It doesn't seem to be aware there is
anything
special about #ifdef 0

Understand For C++ doesn't do this. It marks #ifdefed out code
but not in the same way as commented out code.

Have you been on vacation or something? You're replying
to big buch of threads that dies three weeks ago.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,775
Messages
2,569,601
Members
45,182
Latest member
BettinaPol

Latest Threads

Top