473,406 Members | 2,467 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,406 software developers and data experts.

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

(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
10 2313
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
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
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

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
"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

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

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

Similar topics

48
by: Joseph | last post by:
Hi I'm writing a commercial program which must be reliable. It has to do some basic reading and writing to and from files on the hard disk, and also to a floppy. I have foreseen a potential...
8
by: Lu | last post by:
Hi there, I got a program to write data to a randomly accessed file (the program moves file pointer to a certain position of the file according the current "keyword" and then writes data). It...
11
by: Mr. Smith | last post by:
Hello all, My code can successfully open, write to, format and save several worksheets in a workbook then save it by a given name, close and quit excel. My problem is that if I try and do it...
2
by: Bruce Dodds | last post by:
One of my clients is going to move to CD or DVD as a medium to backup/transfer data. Is it possible for an A2003 application to write directly to a CD or DVD under Win XP, or will I need to set up...
4
by: phantom | last post by:
Hi All. I am having a problem writing to a file. I can successfully open the file and write the contents using fprintf, however if the writing is not done for a while in the process the file...
16
by: iwdu15 | last post by:
how can i open a file i saved and place the info into different text boxes?
0
by: riggor | last post by:
I have been searching for answers to this question ... but all I have found is other people asking the same question..but I have not found an answer... I am trying to install Oracle 10g on...
19
by: rmr531 | last post by:
First of all I am very new to c++ so please bear with me. I am trying to create a program that keeps an inventory of items. I am trying to use a struct to store a product name, purchase price,...
6
by: Shawn | last post by:
Hello: I have the following code in a PHP file. An HTML form passes user comment data to the PHP, which then appends the user comments to the end of the HTML file on which the form is located....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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.