Checking fprintf for errors and testing for existence of a file

J

Jim Hunter

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
 
M

Mark McIntyre

. 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.
 
E

Eric Sosman

Jim said:
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
 
J

Jim Hunter

Eric Sosman said:
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.
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top