Re: questions on ftell and fopen
- From: "Bill Pursell" <bill.pursell@xxxxxxxxx>
- Date: 4 Mar 2007 13:33:51 -0800
On Mar 4, 8:52 pm, Keith Thompson <k...@xxxxxxx> wrote:
"Bill Pursell" <bill.purs...@xxxxxxxxx> writes:
On Mar 3, 8:06 pm, Keith Thompson <k...@xxxxxxx> wrote:
"Bill Pursell" <bill.purs...@xxxxxxxxx> writes:
On Mar 2, 5:52 am, "subramanian10...@xxxxxxxxx, India"
<subramanian10...@xxxxxxxxx> wrote:
Consider the following program:<some code snipped>
if ((fp = fopen(argv[1], "r")) == NULL)
{
printf("Could not open input file :%s:\n", argv[1]);
exit(EXIT_FAILURE);
}
Just a note, you may be much happier replacing the printf line with:
perror(argv[1]);
It will print a much more meaningful message. eg:
[tmp]$ ./a.out foo
foo: No such file or directory
[tmp]$ touch foo; chmod 000 foo; ./a.out foo
foo: Permission denied
It's likely to do so on most systems, but the standard doesn't
actually guarantee that a failing fopen() sets errno.
True. However, it strikes me that it is NOT the
programmer's responsibility to account for inadequacies
of the implementation.
Yes, it is. Whose responsibility do you think it is?
A standard is a contract between the implementer and the programmer.
If an implementer claims his compiler conforms to the C standard, the
programmer may reasonably rely on that claim and make use of any
features guaranteed by the standard. If the programmer relies on
something *not* guaranteed by the standard (in this case, that fopen()
sets errno on failure), and the program misbehaves as a result, who is
responsible for the failure?
We're not talking about a failure here, we're talking about
the quality of an error message. On a good implementation,
perror() will generate a reasonable error message. On
a poor implementation, the error messsage will be less
helpful. Is it any more helpful to output:
"operation failed, and this implementation is so broken
that I can't tell you why." than it is to output a message
based on an incorrect value of errno? (well, yeah, I
guess it is...but not by much.)
I suppose I'll raise my standards and make a point
to only claim POSIX conformance for my code. Time
to remove -ansi from CFLAGS, I guess...
.
- Follow-Ups:
- Re: questions on ftell and fopen
- From: CBFalconer
- Re: questions on ftell and fopen
- From: Chris Torek
- Re: questions on ftell and fopen
- From: Gordon Burditt
- Re: questions on ftell and fopen
- References:
- questions on ftell and fopen
- From: subramanian100in@xxxxxxxxx, India
- Re: questions on ftell and fopen
- From: Bill Pursell
- Re: questions on ftell and fopen
- From: Keith Thompson
- Re: questions on ftell and fopen
- From: Bill Pursell
- Re: questions on ftell and fopen
- From: Keith Thompson
- questions on ftell and fopen
- Prev by Date: Re: questions on ftell and fopen
- Next by Date: Re: Getting list of functions in a header file
- Previous by thread: Re: questions on ftell and fopen
- Next by thread: Re: questions on ftell and fopen
- Index(es):
Relevant Pages
|