473,779 Members | 2,064 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2341
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.i nvalidwrote:
<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.i nvalidwrote:
<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_FAILU RE);
}
}
else
f = stdout;

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

return EXIT_SUCCESS;
}

Dec 14 '06 #5
"matevzb" <ma*****@gmail. comwrites:
On Dec 13, 7:33 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
<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_Keit h) 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_FAILU RE);
}
}
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.orgwr ote:
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,siz eof(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,siz eof(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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

48
8510
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 problem. The program may crash unexpectedly while writing to the file. If so, my program should detect this during startup, and then (during startup) probably delete the data added to the file and redo the writing operation.
8
3343
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 compiles and runs in Win2K and various Unix and Linux systems. The main program is in Fortran, calling a data writing library writen in C++. The weird thing is, it takes much longer elapse time (wall-clock time) than CPU time (e.g., 20 min vs. 2...
11
4057
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 again, Excel hangs. OR if I open Excel again (say from a desktop icon) before I close Access, Excel hangs. (this has happened for both 97 & 2000 for me) I of course thought that I mustn't be unloading a variable properly.
2
1486
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 an external script? TIA, Bruce
4
1512
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 write returns a status -1. To elaborate here is the code segment main (... , ...) {
16
2209
by: iwdu15 | last post by:
how can i open a file i saved and place the info into different text boxes?
0
11775
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 Solaris 10 on x86. When I run the installer I get the following error message: "Error in writing to directory /tmp/OraInstall2006-10-12_05-05-40PM. Please ensure that this directory is writable and has atleast 69 MB of disk space. Installation...
19
4782
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, sell price, and a taxable flag (a Y/N char) and then write this all out to a file (preferably just a plain old text file) and then read it in later so that I can keep a running inventory. The problem that I am running into is when I write to the...
6
2547
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. This PHP code works: the HTML file with added comments displays correctly in my browser. However, appending text to the very end of the HTML file creates what is, strictly speaking, invalid code. I am looking for a way to tell PHP to write data to...
0
9636
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10074
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8961
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6724
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5373
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5503
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4037
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3632
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2869
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.