Afficher un message
Vieux 22/10/2007, 16h36   #7
Keith Thompson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Goto still considered ful

CBFalconer <cbfalconer@yahoo.com> writes:
> Keith Thompson wrote:

[...]
>>> 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. :-)

[snip]
>> 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.


It doesn't distinguish between an error in getc() and reaching
end-of-file.

putc() can fail if, for example, the target file system is full. This
is an important condition to check. Such a failure can also show up
in fclose(), which should also be checked.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
  Réponse avec citation
 
Page generated in 0,05052 seconds with 9 queries