469,576 Members | 1,824 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,576 developers. It's quick & easy.

Checking fprintf for errors and testing for existence of a file

From what I've read, the only indication fprintf gives of an error is a
negative return value. I have a series of writes to a file using
fprintf, and, while I need to know if a failure has occurred, I don't
particularly care which call failed - just that one of them did. I was
thinking I might simply check the return value of the last call to
fprintf. Is it reasonable to assume that if one call to fprintf fails,
then all subsequent calls will fail? Or should I check (shudder) after
every call?

Secondly, what's the best way to portably determine whether a file
exists? The only way I can see to do it is to attempt to open it for
reading, and see if that fails, but then you still aren't guaranteed
that the file doesn't exist.

Thanks in advance.

Jim
Nov 14 '05 #1
3 4735
On 06 Mar 2005 01:08:17 -0600, in comp.lang.c , Jim Hunter
<ne******@paradigmshift.homelinux.net> wrote:
. Is it reasonable to assume that if one call to fprintf fails,
then all subsequent calls will fail?
AFAIK it depends on your OS and hardware I'm afraid. Imagine you couldn't
print because of insufficient disk space, but a background process cleared
some out before the next write.
Or should I check (shudder) after every call?
Its the only safe way.
Secondly, what's the best way to portably determine whether a file
exists? The only way I can see to do it is to attempt to open it for
reading, and see if that fails, but then you still aren't guaranteed
that the file doesn't exist.


You're correct, there's no portable guaranteed way. Not only might the file
exist but be unopenable by you (another process has it, you don't have
permissions etc) but even if you did manage to open it, another process
might remove it or revoke your permissions after your test.

On the other hand, your OS probably has facilities to do this. You'd
typically have to write some conditional preprocessor code to handle
multiple environments.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
Nov 14 '05 #2
Jim Hunter wrote:
From what I've read, the only indication fprintf gives of an error is a
negative return value. I have a series of writes to a file using
fprintf, and, while I need to know if a failure has occurred, I don't
particularly care which call failed - just that one of them did. I was
thinking I might simply check the return value of the last call to
fprintf. Is it reasonable to assume that if one call to fprintf fails,
then all subsequent calls will fail? Or should I check (shudder) after
every call?
You can make multiple "unchecked" calls to fprintf()
and then use ferror() to test whether any of them failed.
You won't know which, but if you don't need to ...
Secondly, what's the best way to portably determine whether a file
exists? The only way I can see to do it is to attempt to open it for
reading, and see if that fails, but then you still aren't guaranteed
that the file doesn't exist.


This is Question 19.11 in the comp.lang.c Frequently
Asked Questions (FAQ) list

http://www.eskimo.com/~scs/C-faq/top.html

--
Eric Sosman
es*****@acm-dot-org.invalid

Nov 14 '05 #3
Eric Sosman <es*****@acm-dot-org.invalid> writes:
You can make multiple "unchecked" calls to fprintf()
and then use ferror() to test whether any of them failed.
You won't know which, but if you don't need to ...


Perfect. Thanks.
Secondly, what's the best way to portably determine whether a file
exists? The only way I can see to do it is to attempt to open it for
reading, and see if that fails, but then you still aren't guaranteed
that the file doesn't exist.


This is Question 19.11 in the comp.lang.c Frequently
Asked Questions (FAQ) list

http://www.eskimo.com/~scs/C-faq/top.html


Thanks. I hadn't finished reading the FAQ. I'll rectify that.
Thanks also to Mark for his very helpful reply.

Jim

--
I was born not knowing and have had only a little time to change
that here and there. -- Richard Feynman
Nov 14 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by GujuBoy | last post: by
14 posts views Thread by Frances Del Rio | last post: by
2 posts views Thread by mike | last post: by
5 posts views Thread by William Payne | last post: by
4 posts views Thread by GGarramuno | last post: by
66 posts views Thread by Johan Tibell | last post: by
1 post views Thread by samac | last post: by
16 posts views Thread by Prayag Narula | last post: by
125 posts views Thread by jacob navia | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.