473,414 Members | 1,703 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

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 4929
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: GujuBoy | last post by:
i want to check to see if a certain program is installed on my windows box using python. how can i do that...(ie, i want to see if "word" is installed) please help
14
by: Frances Del Rio | last post by:
if (parent.frames.main.location == 'mediaselect.html') { I have a very simple frameset, name of frame where I'm checking is 'main'... why is this not working? I mean this is correct syntax,...
2
by: mike | last post by:
I had a form like below that validated that a file was there before it would submit. <form name="attach" method="POST" action="run_this_pgm.cfm" enctype="multipart/form-data"...
5
by: William Payne | last post by:
Hello, I am in the process of converting a C++ program to a C program. The user of the program is supposed to supply an integer on the command line and in the C++ version of the program I was using...
4
by: GGarramuno | last post by:
I have a program that expects its input in a specific format. Mainly, it expects floating-point values to be formatted in the form: 1.32 1. 3. 3.2345 In case you missed it, all floating...
66
by: Johan Tibell | last post by:
I've written a piece of code that uses sockets a lot (I know that sockets aren't portable C, this is not a question about sockets per se). Much of my code ended up looking like this: if...
1
by: samac | last post by:
Hi all, Need some help in c. I have not been in programming for ten years. Here is my problem: From main(), I call a function - a_function(). In a_function(), I call another function...
16
by: Prayag Narula | last post by:
Hi, I want to redefine fprintf for debugging purposes. That is I want that all the output that is going to the stdout should be logged in a file. I tried something like #define fprintf...
125
by: jacob navia | last post by:
We hear very often in this discussion group that bounds checking, or safety tests are too expensive to be used in C. Several researchers of UCSD have published an interesting paper about this...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.