J
Joachim Schmitz
Peter Pichler said:Alzheimer? Isn't that the German guy who follows me around and hides
things?
http://en.wikipedia.org/wiki/Alois_Alzheimer
Peter Pichler said:Alzheimer? Isn't that the German guy who follows me around and hides
things?
So look at the following, with a single return, and no complexity:
int fcopy(const char *dst, const char *src) {
FILE *sf, *df;
int err, ch;
if (!(sf = fopen(src, "r"))) err = 1;
else if (!(df = fopen(dst, "w"))) err = 2;
else {
while (EOF != (ch = getc(sf))) putc(ch, df);
err = 0;
You don't need all that confusing local declaration of variables,
etc. Consider:
int fcopy(char const *dst, char const *src) {
FILE *sf, *df;
int ch
if (sf = fopen(src, "r") { /* text files */
if (df = fopen(dst, "w") {
while (EOF != (ch = getc(sf))) putc(ch, df);
fclose(df);
}
fclose(sf);
}
return sf && df; /* non-zero for success */
}
which I consider more readable and safer than your version.![]()
Tor Rustad said:CBFalconer wrote:
[...]
You don't need all that confusing local declaration of variables,
etc. Consider:
int fcopy(char const *dst, char const *src) {
FILE *sf, *df;
int ch
if (sf = fopen(src, "r") { /* text files */
if (df = fopen(dst, "w") {
while (EOF != (ch = getc(sf))) putc(ch, df);
fclose(df);
}
fclose(sf);
}
return sf && df; /* non-zero for success */
}
which I consider more readable and safer than your version.![]()
This is good-weather code, and has nothing to do with "safe" code in
production.
Just on my first read, I think you need to consider:
- fopen failure
- getc failure
- putc failure
far more carefully.
Keith said:Tor Rustad said:CBFalconer wrote:
[...]
This is good-weather code, and has nothing to do with "safe" code inYou don't need all that confusing local declaration of variables,
etc. Consider:
int fcopy(char const *dst, char const *src) {
FILE *sf, *df;
int ch
if (sf = fopen(src, "r") { /* text files */
if (df = fopen(dst, "w") {
while (EOF != (ch = getc(sf))) putc(ch, df);
fclose(df);
}
fclose(sf);
}
return sf && df; /* non-zero for success */
}
which I consider more readable and safer than your version.![]()
production.
Just on my first read, I think you need to consider:
- fopen failure
- getc failure
- putc failure
far more carefully.
I believe it already handles fopen failure correctly.
Keith said:Tor Rustad said:CBFalconer wrote:
[...]
You don't need all that confusing local declaration of variables,
etc. Consider:
int fcopy(char const *dst, char const *src) {
FILE *sf, *df;
int ch
if (sf = fopen(src, "r") { /* text files */
if (df = fopen(dst, "w") {
while (EOF != (ch = getc(sf))) putc(ch, df);
fclose(df);
}
fclose(sf);
}
return sf && df; /* non-zero for success */
}
which I consider more readable and safer than your version.![]()
This is good-weather code, and has nothing to do with "safe" code in
production.
Just on my first read, I think you need to consider:
- fopen failure
- getc failure
- putc failure
far more carefully.
I believe it already handles fopen failure correctly.
I agree about getc and putc.
Keith said:Tor Rustad said:CBFalconer wrote:
[...]
You don't need all that confusing local declaration of variables,
etc. Consider:
int fcopy(char const *dst, char const *src) {
FILE *sf, *df;
int ch
if (sf = fopen(src, "r") { /* text files */
if (df = fopen(dst, "w") {
while (EOF != (ch = getc(sf))) putc(ch, df);
fclose(df);
}
fclose(sf);
}
return sf && df; /* non-zero for success */
}
which I consider more readable and safer than your version.![]()
This is good-weather code, and has nothing to do with "safe" code in
production.
Just on my first read, I think you need to consider:
- fopen failure
- getc failure
- putc failure
far more carefully.
I believe it already handles fopen failure correctly.
I agree about getc and putc.
santosh said:Keith said:Tor Rustad said:CBFalconer wrote:
[...]
You don't need all that confusing local declaration of
variables, etc. Consider:
int fcopy(char const *dst, char const *src) {
FILE *sf, *df;
int ch
if (sf = fopen(src, "r") { /* text files */
if (df = fopen(dst, "w") {
while (EOF != (ch = getc(sf))) putc(ch, df);
fclose(df);
}
fclose(sf);
}
return sf && df; /* non-zero for success */
}
which I consider more readable and safer than your version.
This is good-weather code, and has nothing to do with "safe"
code in production.
Just on my first read, I think you need to consider:
- fopen failure
- getc failure
- putc failure
far more carefully.
I believe it already handles fopen failure correctly.
I agree about getc and putc.
The code snippet wont compile.
CBFalconer said:I am not concerned with debuggery. I haven't used one for two
years. I am concerned with readability, and to me good readability
requires minimizing vertical creep and expansion. Thus I will
normally use the 'one line' condition and single action statement
construct. Also, should I really need to delve into the debugged
action of such a statement, I can easily alter the source and
recompile.
I think most people will, on rereading my original post (quoted
above), agree that the code is clear. Each line performs a
function, possibly combined with a test. The alternative paths are
clear. I consider it an excellent style example. Richards
overconcern with fixed and pointless formatting dicta will simply
cause confusion.
CBFalconer said:Keith Thompson wrote: [...][snip]CBFalconer wrote:
[...]
You don't need all that confusing local declaration of variables,
etc. Consider:
int fcopy(char const *dst, char const *src) {
FILE *sf, *df;
int ch
if (sf = fopen(src, "r") { /* text files */
if (df = fopen(dst, "w") {
while (EOF != (ch = getc(sf))) putc(ch, df);
fclose(df);
}
fclose(sf);
}
return sf && df; /* non-zero for success */
}
which I consider more readable and safer than your version.![]()
I believe it already handles fopen failure correctly.
I agree about getc and putc.
It also handles getc failure. putc failure is not handled. The
handling technique is simply to abort the function execution.
fopen failure is signalled, the others are not.
putc can be handled by "if (EOF != putc(ch, df)) {
/* error action */
break;
}"
and similar testing after the while loop can give details about a
getc failure. Most systems don't have any use for all this, and
function exit suffices.
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.