Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #160097
| From | Tim Rentsch <tr.17687@z991.linuxsc.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Which side of bitwise OR is evaluated first? |
| Date | 2021-04-14 08:06 -0700 |
| Organization | A noiseless patient Spider |
| Message-ID | <8635vsj46u.fsf@linuxsc.com> (permalink) |
| References | <a21102b2-7c82-40d9-9549-c10a1b77247en@googlegroups.com> |
Oğuz <oguzismailuysal@gmail.com> writes:
> I wrote this:
>
> fp = fopen(optarg, "r");
> if (!fp || (readfrom(fp), ferror(fp)|fclose(fp)))
> perror(optarg)
>
> `readfrom' is a function of type `void' that calls `getline' until
> it returns -1.
>
> The reason why I wrote it this way is that I wanted to handle all
> errors and close the file pointer in a single block. But I'm not
> sure if `ferror' is guaranteed to be called before `fclose' there,
> and the standard says once `fclose' is called on `fp', what
> `ferror(fp)' will do is unspecified or something like that.
>
> So, should I change this or is it good (except being a bit
> unreadable)?
Two comments.
One, don't listen to the advice of "experts" who say something
is or is not readable. Instead try writing the code several
different ways and pick the one that best expresses what it
is you want to say.
Two, for what you're doing here I would be inclined to try something
like this:
FILE *fp = fopen( optarg, "r" );
int read_bad = fp ? readfrom( fp ), ferror( fp ) : 0;
int close_bad = fp ? fclose( fp ) : 0;
if( !fp || read_bad || close_bad ) perror( optarg );
Written this way I find it easy to see at a glance what the code is
trying to do, and what the condition is to control whether an error
message should be printed.
If that approach works for you, great. The key though is to look
for a way of writing that most closely expresses what it is you
want to say.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Which side of bitwise OR is evaluated first? Oğuz <oguzismailuysal@gmail.com> - 2021-04-13 22:43 -0700
Re: Which side of bitwise OR is evaluated first? Barry Schwarz <schwarzb@delq.com> - 2021-04-14 00:04 -0700
Re: Which side of bitwise OR is evaluated first? David Brown <david.brown@hesbynett.no> - 2021-04-14 09:42 +0200
Re: Which side of bitwise OR is evaluated first? Oğuz <oguzismailuysal@gmail.com> - 2021-04-14 00:51 -0700
Re: Which side of bitwise OR is evaluated first? David Brown <david.brown@hesbynett.no> - 2021-04-14 11:25 +0200
Re: Which side of bitwise OR is evaluated first? Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2021-04-14 03:27 -0700
Re: Which side of bitwise OR is evaluated first? David Brown <david.brown@hesbynett.no> - 2021-04-14 13:28 +0200
Re: Which side of bitwise OR is evaluated first? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-04-14 10:31 -0700
Re: Which side of bitwise OR is evaluated first? David Brown <david.brown@hesbynett.no> - 2021-04-14 19:40 +0200
Re: Which side of bitwise OR is evaluated first? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-04-14 10:26 -0700
Re: Which side of bitwise OR is evaluated first? Richard Damon <Richard@Damon-Family.org> - 2021-04-14 07:22 -0400
Re: Which side of bitwise OR is evaluated first? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2021-04-14 07:54 -0700
Re: Which side of bitwise OR is evaluated first? James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-04-14 14:23 -0400
Re: Which side of bitwise OR is evaluated first? scott@slp53.sl.home (Scott Lurndal) - 2021-04-14 14:33 +0000
Re: Which side of bitwise OR is evaluated first? James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-04-14 13:15 -0400
Re: Which side of bitwise OR is evaluated first? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2021-04-14 08:06 -0700
Re: Which side of bitwise OR is evaluated first? Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2021-04-14 10:34 -0600
Re: Which side of bitwise OR is evaluated first? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2021-07-11 00:24 -0700
csiph-web