how to check for out of disk space

R

rtillmore

Hi,

I have a program that writes data to a file. I need to check for out
of disk space and other disk related errors. My code looks like:

if ((optr = fopen(filename,"w")) == NULL) {
printf("could not open %s\n", filename);
return -1;
}
else {
fprintf(optr,"%s\n",output);
while ((doneyet(output,next)==0) && (status==0)) {
nextone(output,next);
status=fprintf(optr,"%s\n",output); //out of disk space
}
if (fclose(optr) != 0) {
printf("fclose error number = %d\n",errno);
return -1;
}
if (status !=0) {
printf("fprintf error = %d\n",status);
return -1;
}
}

So far I have run out of disk space during the while loop and my
program just keeps running. I want it to say out of disk space and
exit gracefully. With the code above I just get fprint error = -1.
feof only returns 0 or 1 so that won't work. What is the best way to
check for the out of disk space error condition?

Thanks,
 
K

Keith Thompson

I have a program that writes data to a file. I need to check for out
of disk space and other disk related errors. My code looks like:

if ((optr = fopen(filename,"w")) == NULL) {
printf("could not open %s\n", filename);
return -1;
}
else {
fprintf(optr,"%s\n",output);
while ((doneyet(output,next)==0) && (status==0)) {
nextone(output,next);
status=fprintf(optr,"%s\n",output); //out of disk space
}
if (fclose(optr) != 0) {
printf("fclose error number = %d\n",errno);
return -1;
}
if (status !=0) {
printf("fprintf error = %d\n",status);
return -1;
}
}

So far I have run out of disk space during the while loop and my
program just keeps running. I want it to say out of disk space and
exit gracefully. With the code above I just get fprint error = -1.
feof only returns 0 or 1 so that won't work. What is the best way to
check for the out of disk space error condition?

You appear to be assuming that fprintf() returns 0 on success. In
fact it returns the number of characters printed, or a negative value
if an output or encoding error occurred.
 
R

rtillmore

You appear to be assuming that fprintf() returns 0 on success.  In
fact it returns the number of characters printed, or a negative value
if an output or encoding error occurred.

--
Keith Thompson (The_Other_Keith) (e-mail address removed)  <http://www.ghoti.net/~kst>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

Yeah that was a typo. It really is
if (status < 0) {
printf("fprintf error = %d\n",status);
return -1;
}
 
R

rtillmore

     What you're doing can't be right.  fprintf() returns the number
of characters it wrote if all goes well, or a negative value if
something goes wrong.  The format you're using tries to output at
least one character, so there's no way your fprintf() can return zero:
either it succeeds and returns strlen(output)+1, or it fails and
returns a negative number.  The code you've shown should execute
the while loop either once or not at all, depending on the initial
value of status.  Are you sure this is the code you're running?

     To the larger question, there's more than one way to check for
an output error.  One of them might be something like

        status = fprintf (optr, "%s\n", output);
        while ( status >= 0 && doneyet(output,next) == 0) {
            nextone (output, next);
            status = fprintf (optr, "%s\n", output);
        }
        ...

(I wrote ">= 0" even though I just got through telling you zero
couldn't happen, because if you ever changed the format string
zero *could* happen.)  Another possibility is to use the ferror()
function

     Be aware that this doesn't really check for "out of disk
space," but for *any* kind of I/O error: disk failure, socket
disconnect, gremlin intervention, whatever.  C tells you that
a failure occurred, but can't really explain the underlying
cause(s) -- errno is about as far as it can go.

That is what I get typing code from memory and not cutting and
pasting.
Yes you are correct and the code for the while loop is really:
while ((doneyet(output,next)==0) && (status>=0)) {

According to the documentation I can find on the web fprintf doesn't
set errno. ferror looks promising maybe something like the following:
(warning not compile tested)

if ((optr = fopen(filename,"w")) == NULL) {
printf("could not open %s\n", filename);
return -1;
}
else {
fprintf(optr,"%s\n",output);
while ((doneyet(output,next)==0) && (ferror(optr)==0)) {
nextone(output,next);
fprintf(optr,"%s\n",output); //out of disk space
}
if (fclose(optr) != 0) {
printf("fclose error number = %d\n",errno);
return -1;
}
if (ferror !=0) {
printf("ferror = %d\n",errno);
printf("error text = %s", strerror(errno));
return -1;
}
}
 
E

Eric Sosman

That is what I get typing code from memory and not cutting and
pasting.

No, that is what *we* got *from you* thanks to your
sloppy transcription. *Please* don't do it again. Thanks.
According to the documentation I can find on the web fprintf doesn't
set errno.

The C Standard does not require fprintf() to set errno,
but it permits it to do so. Also, other standards that may
apply to your environment(s) of interest may have something
to say on the matter.
 
R

rtillmore

     No, that is what *we* got *from you* thanks to your
sloppy transcription.  *Please* don't do it again.  Thanks.


     The C Standard does not require fprintf() to set errno,
but it permits it to do so.  Also, other standards that may
apply to your environment(s) of interest may have something
to say on the matter.

I started checking ferror and I now get out of disk space messages.
The code is slightly modified from above.

Thanks,
 

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

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top