473,763 Members | 7,719 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

fwrite() does not write data to file

WANTED: Even if I do Ctrl-C in the middle of fgets(), fwrite() should
write the previously entered data to a file (except if I hit the file-size
limit)
PROBLEM: If I do a Ctrl-C in the middle of fgets(). fwrite() does not
write the data to the file.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

enum { INPUT_SIZE = 5 };
FILE* fp;

void write_to_file( const char* );
int main( void )
{
char arrc[INPUT_SIZE];

memset( arrc, '\0', INPUT_SIZE );

while( fgets(arrc, INPUT_SIZE, stdin) )
{
write_to_file( arrc );
}
return EXIT_SUCCESS;
}


void write_to_file( const char* arrc )
{
int arr_size;
long fwrite_bytes;

arr_size = strlen(arrc );
++arr_size;

if( ! (fp = fopen("zzz.txt" , "a")) )
{
perror("FOPEN ERROR\n");
exit( EXIT_FAILURE );
}

fwrite_bytes = fwrite( arrc, 1, arr_size, fp);

printf("fwrite_ bytes = %ld\n", fwrite_bytes);

if( arr_size != fwrite_bytes )
{
perror("FWRITE ERROR");
exit( EXIT_FAILURE );
}

/*
if( fclose(fp) )
{
perror("CLOSE ERROR\n");
}
*/
}
=============== OUTPUT =============== ======
[arnuld@dune CLS]$ gcc -ansi -pedantic -Wall -Wextra check_FILE_IO.c
[arnuld@dune CLS]$ ./a.out
lo
fwrite_bytes = 4

[arnuld@dune CLS]$ cat zzz.txt
[arnuld@dune CLS]$
In only these 3 cases, data gets written:

1) Remove the comments from the fclose(). I mean do a proper fclose().
2) You do proper exit using Ctrl-D.
3) User enters data more than the INPUT_SIZE.

but I don't want to close the file every time I have data. I want to keep
it open till I hit the size limit. The problem with fclose() is, if the
data entered is 2 bytes on each call, then it will take 500 openings and
closings, which will be very CPU intensive I think. I want this
program to be efficient in terms of CPU, memory is not the problem
here, I have got enough of it. I need to keep the file open but in doing
so a sudden quit using Ctrl-C discards everything user entered.
Any solution to the problem ?


--
www.lispmachine.wordpress.com
my email is @ the above blog.Google Groups is Blocked. Reason: Excessive Spamming

Sep 2 '08 #1
12 8976
On Sep 2, 10:50 am, arnuld <sunr...@invali d.addresswrote:
WANTED: Even if I do Ctrl-C in the middle of fgets(), fwrite() should
write the previously entered data to a file (except if I hit the file-size
limit)

PROBLEM: If I do a Ctrl-C in the middle of fgets(). fwrite() does not
write the data to the file.
^C typically kills the process. It's not possible to do what you want.

<snip>
Sep 2 '08 #2
arnuld <su*****@invali d.addresswrites :
[...]
but I don't want to close the file every time I have data. I want to keep
it open till I hit the size limit. The problem with fclose() is, if the
data entered is 2 bytes on each call, then it will take 500 openings and
closings, which will be very CPU intensive I think. I want this
program to be efficient in terms of CPU, memory is not the problem
here, I have got enough of it. I need to keep the file open but in doing
so a sudden quit using Ctrl-C discards everything user entered.
Any solution to the problem ?
fflush().

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <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"
Sep 2 '08 #3
arnuld said:
WANTED: Even if I do Ctrl-C in the middle of fgets(), fwrite() should
write the previously entered data to a file (except if I hit the
file-size limit)
Implementations are permitted to buffer streams, in which case an arbitrary
program termination may result in data in the buffer not being written to
the associated file.

To ensure (as far as possible, anyway) that data sent to fwrite is written
to the file, call fflush after fwrite:

fwrite_bytes = fwrite( arrc, 1, arr_size, fp);
if(fflush(fp) == EOF)
{
something went wrong
PROBLEM: If I do a Ctrl-C in the middle of fgets(). fwrite() does not
write the data to the file.
If you mean the data processed by previous fgets calls, fflush should fix
that.

If you mean data processed by /this/ fgets call, the one that you've just
interrupted, it isn't possible to do that in standard C - not even if you
mess with signal-handling - since there is no obligation on fgets to have
placed any data at all into your buffer at the point of interruption. It
only has to have done so by the time it returns, and it can't return (in
the normal sense) if you interrupt it.

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Sep 2 '08 #4
On Tue, 02 Sep 2008 08:13:07 +0000, Richard Heathfield wrote:
.... SNIP....
fwrite_bytes = fwrite( arrc, 1, arr_size, fp);
if(fflush(fp) == EOF)
{
something went wrong
If you mean the data processed by previous fgets calls, fflush should
fix that.

yes, I meant exactly that and hey... Richard, it works :)
--
www.lispmachine.wordpress.com
my email is @ the above blog.
Google Groups is Blocked. Reason: Excessive Spamming

Sep 2 '08 #5
vi******@gmail. com wrote:
On Sep 2, 10:50 am, arnuld <sunr...@invali d.addresswrote:
>WANTED: Even if I do Ctrl-C in the middle of fgets(), fwrite()
should write the previously entered data to a file (except if I hit
the file-size limit)

PROBLEM: If I do a Ctrl-C in the middle of fgets(). fwrite() does
not write the data to the file.

^C typically kills the process. It's not possible to do what you want.
^C typically (in UNIX) sends a SIGINT, which you could catch or even ignor.
But that would be off topic here...

Bye, Jojo
Sep 2 '08 #6
Joachim Schmitz said:
vi******@gmail. com wrote:
>On Sep 2, 10:50 am, arnuld <sunr...@invali d.addresswrote:
>>WANTED: Even if I do Ctrl-C in the middle of fgets(), fwrite()
should write the previously entered data to a file (except if I hit
the file-size limit)

PROBLEM: If I do a Ctrl-C in the middle of fgets(). fwrite() does
not write the data to the file.

^C typically kills the process. It's not possible to do what you want.

^C typically (in UNIX) sends a SIGINT, which you could catch or even
ignor. But that would be off topic here...
Why? SIGINT is a standard signal. See 4.7 of C89 or 7.14 of C99.

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Sep 2 '08 #7
Richard Heathfield wrote:
Joachim Schmitz said:
>vi******@gmail. com wrote:
>>On Sep 2, 10:50 am, arnuld <sunr...@invali d.addresswrote:
WANTED: Even if I do Ctrl-C in the middle of fgets(), fwrite()
should write the previously entered data to a file (except if I hit
the file-size limit)

PROBLEM: If I do a Ctrl-C in the middle of fgets(). fwrite() does
not write the data to the file.

^C typically kills the process. It's not possible to do what you
want.

^C typically (in UNIX) sends a SIGINT, which you could catch or even
ignor. But that would be off topic here...

Why? SIGINT is a standard signal. See 4.7 of C89 or 7.14 of C99.
Well, because I thought it to be POSIX.

Thanks for the correction.

Bye, Jojo
Sep 2 '08 #8
On Sep 2, 7:31 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
Joachim Schmitz said:
vipps...@gmail. com wrote:
On Sep 2, 10:50 am, arnuld <sunr...@invali d.addresswrote:
WANTED: Even if I do Ctrl-C in the middle of fgets(), fwrite()
should write the previously entered data to a file (except if I hit
the file-size limit)
>PROBLEM: If I do a Ctrl-C in the middle of fgets(). fwrite() does
not write the data to the file.
^C typically kills the process. It's not possible to do what you want.
^C typically (in UNIX) sends a SIGINT, which you could catch or even
ignor. But that would be off topic here...

Why? SIGINT is a standard signal. See 4.7 of C89 or 7.14 of C99.
SIGINT is. What ^C does (or ^C itself) is not standard though. (not
even in POSIX)
Sep 2 '08 #9
On Tue, 02 Sep 2008 14:18:09 -0700, vippstar wrote:
On Sep 2, 7:31 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
>Joachim Schmitz said:
^C typically (in UNIX) sends a SIGINT, which you could catch or even
ignor. But that would be off topic here...

Why? SIGINT is a standard signal. See 4.7 of C89 or 7.14 of C99.

SIGINT is.
True, but it's worth pointing out that in ISO C, there is significantly
less that you are allowed to do in the signal handler.
What ^C does (or ^C itself) is not standard though. (not even
in POSIX)
<OTThat depends on what you mean. What ^C does is specified by POSIX,
but whether it is triggered by ^C is not. </OT>
Sep 2 '08 #10

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

Similar topics

2
2333
by: lawrence | last post by:
I'm probably missing something obvious, but I'm unable to write a file with this function. I've used my FTP software to set permissions to 777 on all the files in question. I've tried r, r+, w, and w+ as possible ways of opening the files. The first fopen is trying to create a file where none exists. It fails (thought I've set dir permissions to 777 as well). How can I create a file? There error messages print back the full input, so...
6
16618
by: Tony C | last post by:
Does Python have a function that is analogous to C's write() or fwrite()- that is , I want to write a file (of arbitrary data) that is 100K, or 1MB (or more) bytes long.. Both write() and fwrite() in C allow the user to specify the size of the data to be written. Python's write only allows a string to be passed in.
2
4116
by: SunX | last post by:
Question from a newbie. How do you write out a data file of floating point numbers? file.write only takes strings. Many thanks.
6
8534
by: leonecla | last post by:
Hi everybody, I'm facing a very very strange problem with a very very simple C program... My goal should be to write to a binary file some numbers (integers), each one represented as a sequence of 32 bit. I made this stupid trial code: --------------------------------------------- FILE *fout;
2
4907
by: ykgoh | last post by:
Hi. I've a problem of being able to create and remove a directory but unable to write a file inside the created directory for some strange reason. I suspect that this problem could be vaguely linked to Safe mode being set to On since my site is using shared server hosting and probably insufficient/incorrect Unix file permission. Below is my test script that helps me narrow down the problem....
0
789
by: Buddy Home | last post by:
Hello, I'm trying to upload a file programatically and occasionally I get the following error message. Unable to write data to the transport connection: An established connection was aborted by the software in your host machine. Stack Trace at System.Net.Sockets.NetworkStream.Write(Byte buffer, Int32 offset, Int32
3
14055
by: Buddy Home | last post by:
Hello, I'm trying to upload a file programatically and occasionally I get the following error message. Unable to write data to the transport connection: An established connection was aborted by the software in your host machine. Stack Trace at System.Net.Sockets.NetworkStream.Write(Byte buffer, Int32 offset, Int32
6
7379
by: ericunfuk | last post by:
Hi ALL, I want to read a binary file(it's pic.tif file, I guess it's binary file?), then write it to a new file), I have several questions about this process: When I use fread() to read a chunk of the file into a buffer, when it encounters the end of the file, will the EOF indicator be put into the buffer automatically just as an ordinary byte of the file, or do I have to do it manually?
24
4456
by: Bill | last post by:
Hello, I'm trying to output buffer content to a file. I either get an access violation error, or crazy looking output in the file depending on which method I use to write the file. Can anyone help out a newbie? #include <stdio.h> #include <ctype.h> #include <string.h>
0
9386
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9997
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9822
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8821
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...
1
7366
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3917
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
3
3522
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2793
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.