PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > Re: Goto still considered ful
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Re: Goto still considered ful

Réponse
 
LinkBack Outils de la discussion
Vieux 20/10/2007, 20h12   #1
Tor Rustad
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Goto still considered ful

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.

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

"I have stopped reading Stephen King novels. Now I just read C code instead"
  Réponse avec citation
Vieux 20/10/2007, 21h00   #2
Keith Thompson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Goto still considered ful

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.

I agree about getc and putc.

--
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
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
Vieux 20/10/2007, 21h36   #4
santosh
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.
>
> I agree about getc and putc.
>


The code snippet wont compile.

  Réponse avec citation
Vieux 20/10/2007, 22h54   #5
CBFalconer
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.
>
> 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.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>



--
Posted via a free Usenet account from http://www.teranews.com

  Réponse avec citation
Vieux 20/10/2007, 22h59   #6
CBFalconer
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Goto still considered ful

santosh wrote:
> 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.
>>
>> I agree about getc and putc.

>
> The code snippet wont compile.


Yes, there are a couple of missing ')'s. Penalty for typing into
the newsreader. The above errors (apart from putc) already force
function termination.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>



--
Posted via a free Usenet account from http://www.teranews.com

  Réponse avec citation
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
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 22h46.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,17459 seconds with 15 queries