Writing to the hard disk, and checking for error to exit(1)?

E

eastcoastguyz

(I've not done C prorgamming in such a long time, I have forgotten how
to do this.)

I'm writing a program, where it fopens a file with "a+", and I want to
be able to continue writing to this file, but I want the program to
exit(1) if the disk is full or some other write failure. I would like
to see a C code example. Thanks in advance!
 
R

Richard Heathfield

eastcoastguyz said:
(I've not done C prorgamming in such a long time, I have forgotten how
to do this.)

I'm writing a program, where it fopens a file with "a+", and I want to
be able to continue writing to this file, but I want the program to
exit(1) if the disk is full or some other write failure. I would like
to see a C code example. Thanks in advance!

Look up ferror(). Cf <stdio.h>

Also consider using EXIT_FAILURE instead of 1. Cf <stdlib.h>
 
M

matevzb

Look up ferror(). Cf <stdio.h>

Also consider using EXIT_FAILURE instead of 1. Cf <stdlib.h>
There was (still is?) an issue with HPUX where fprintf() would block if
the disk got full, so this might not always work. Does this behaviour
conform to the Standard? I only found "The fprintf function returns the
number of characters transmitted, or a negative value if an output or
encoding error occurred."
 
G

Guest

matevzb said:
There was (still is?) an issue with HPUX where fprintf() would block if
the disk got full, so this might not always work. Does this behaviour
conform to the Standard? I only found "The fprintf function returns the
number of characters transmitted, or a negative value if an output or
encoding error occurred."

It can conform to the standard. But then again, an implementation that
waits three days after each statement can also conform to the standard.
 
B

Bill Pursell

eastcoastguyz said:
(I've not done C prorgamming in such a long time, I have forgotten how
to do this.)

I'm writing a program, where it fopens a file with "a+", and I want to
be able to continue writing to this file, but I want the program to
exit(1) if the disk is full or some other write failure. I would like
to see a C code example. Thanks in advance!

#include <stdlib.h>
#include <stdio.h>

int
main(int argc, char **argv)
{

FILE *f;
char *filename;
const char msg[] = "This is the message\n";

filename = argc > 1 ? argv[1] : NULL;

if (filename != NULL) {
if ( (f = fopen(filename, "a+")) == NULL) {
perror (filename);
exit(EXIT_FAILURE);
}
}
else
f = stdout;

if (fwrite(msg, sizeof *msg, sizeof msg, f)
!= sizeof msg) {
perror(filename);
exit(EXIT_FAILURE);
}

return EXIT_SUCCESS;
}
 
K

Keith Thompson

matevzb said:
Look up ferror(). Cf <stdio.h>

Also consider using EXIT_FAILURE instead of 1. Cf <stdlib.h>
[signature snipped]
There was (still is?) an issue with HPUX where fprintf() would block if
the disk got full, so this might not always work. Does this behaviour
conform to the Standard? I only found "The fprintf function returns the
number of characters transmitted, or a negative value if an output or
encoding error occurred."

It might be conforming. Perhaps running out of disk space is not
considered an error. If fprintf() blocks if the disk is full, but
resumes when (and if) the disk is no longer full, that's probably
conforming behavior. (Treating it as an immediate error is also
conforming behavior.)

I presume this applies to any output routine, not just to fprintf.
(Having fprintf be the only routine that behaves this way would be
very odd, but not necessarily illegal.)
 
B

Bill Pursell

Bill said:
eastcoastguyz said:
(I've not done C prorgamming in such a long time, I have forgotten how
to do this.)

I'm writing a program, where it fopens a file with "a+", and I want to
be able to continue writing to this file, but I want the program to
exit(1) if the disk is full or some other write failure. I would like
to see a C code example. Thanks in advance!

#include <stdlib.h>
#include <stdio.h>

int
main(int argc, char **argv)
{

FILE *f;
char *filename;
const char msg[] = "This is the message\n";

filename = argc > 1 ? argv[1] : NULL;

if (filename != NULL) {
if ( (f = fopen(filename, "a+")) == NULL) {
perror (filename);
exit(EXIT_FAILURE);
}
}
else
f = stdout;

if (fwrite(msg, sizeof *msg, sizeof msg, f)
!= sizeof msg) {

Oops. That should be:
fwrite(msg, sizeof msg, 1, f). The above only
works as long as sizeof *msg == 1. (which is true
as long as msg is a char array, but in isolation the
above line is incorrect.)
 
M

matevzb

It might be conforming. Perhaps running out of disk space is not
considered an error. If fprintf() blocks if the disk is full, but
resumes when (and if) the disk is no longer full, that's probably
conforming behavior. (Treating it as an immediate error is also
conforming behavior.)
Yes it does resume, but this also makes it a part of the problem (this
is probably OT, but I see it as a C issue). Let's say my program opens
a file on disk and starts writing (it's also the only file on that
specific disk). When the disk becomes full the program will block but I
won't be able to free up the space - the program has the file open
there and we're in for a deadlock.
I presume this applies to any output routine, not just to fprintf.
(Having fprintf be the only routine that behaves this way would be
very odd, but not necessarily illegal.)
Although I haven't tried other ones yet, I would presume the same.
 
K

Kohn Emil Dan

On Thu, 13 Dec 2006, Bill Pursell wrote:

Oops. That should be:
fwrite(msg, sizeof msg, 1, f). The above only
works as long as sizeof *msg == 1. (which is true
as long as msg is a char array, but in isolation the
above line is incorrect.)

In this concrete example, I'd rather say

if (fwrite(msg,sizeof(msg)-1,1,f) != sizeof(msg)-1) {
.....

to avoid writing the terminating NUL character.


Emil
 
R

Richard Heathfield

Kohn Emil Dan said:

In this concrete example, I'd rather say

if (fwrite(msg,sizeof(msg)-1,1,f) != sizeof(msg)-1) {
.....

to avoid writing the terminating NUL character.

If your intent is to write a string up to but not including its terminating
null character, then you'll want to use strlen rather than sizeof.
 
R

Roland Pibinger

If your intent is to write a string up to but not including its terminating
null character, then you'll want to use strlen rather than sizeof.

Also, 'fclose' with checked return value would be nice for code that
'fopen's a file and tries to detect "if the disk is full or some other
write failure" (fflush with checked return value may be helpful, too).


Best regards,
Roland Pibinger
 

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,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top