Afficher un message
Vieux 20/10/2007, 21h14   #3
Tor Rustad
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Goto still considered ful

Keith Thompson wrote:
> Tor Rustad <tor_rustad@hotmail.com> writes:
>> 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.


Hmm... I really hate C code like this:

return sf && df;

OK, I see. 'sf' will be NULL on the first fopen failure, and 'df' is not
evaluated. <g>

--
Tor <torust [at] online [dot] no>

"I have stopped reading Stephen King novels. Now I just read C code instead"
  Réponse avec citation
 
Page generated in 0,05133 seconds with 9 queries