By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,903 Members | 1,439 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,903 IT Pros & Developers. It's quick & easy.

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

P: n/a
(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!

Dec 13 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a
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>

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Dec 13 '06 #2

P: n/a
On Dec 13, 7:33 pm, Richard Heathfield <r...@see.sig.invalidwrote:
<snip>
Look up ferror(). Cf <stdio.h>

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

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999http://www.cpax.org.uk
email: rjh at the above domain, - www.
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."
--
WYCIWYG - what you C is what you get

Dec 13 '06 #3

P: n/a
matevzb wrote:
On Dec 13, 7:33 pm, Richard Heathfield <r...@see.sig.invalidwrote:
<snip>
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."
It can conform to the standard. But then again, an implementation that
waits three days after each statement can also conform to the standard.

Dec 14 '06 #4

P: n/a

eastcoastguyz wrote:
(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;
}

Dec 14 '06 #5

P: n/a
"matevzb" <ma*****@gmail.comwrites:
On Dec 13, 7:33 pm, Richard Heathfield <r...@see.sig.invalidwrote:
<snip>
>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.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Dec 14 '06 #6

P: n/a

Bill Pursell wrote:
eastcoastguyz wrote:
(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.)

Dec 14 '06 #7

P: n/a
On Dec 14, 12:29 am, Keith Thompson <k...@mib.orgwrote:
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.
--
WYCIWYG - what you C is what you get

Dec 14 '06 #8

P: n/a

On Thu, 13 Dec 2006, Bill Pursell wrote:

<SNIPPED>
>>
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.)
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
Dec 14 '06 #9

P: n/a
Kohn Emil Dan said:

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

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Dec 14 '06 #10

P: n/a
On Thu, 14 Dec 2006 13:32:55 +0000, Richard Heathfield wrote:
>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
Dec 14 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.