469,963 Members | 1,893 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,963 developers. It's quick & easy.

not writing to file

I try to write some data to a file. After some data had supposedly
been written to the file, I opened the file before the program
closed it and found it empty. What's wrong with my code? Thanks.

-----------only the relevant code is listed-----------------
FILE *lptr
char *name;
int nNo;

..............
..............

lptr=fopen("myfile","w")

...............
...............

/* deep in a function */

sprintf(buffer, "%s\r\n",name);
printf("No is %d. Name is %s\n",nNo,buffer);

/* they printed OK on screen, my way of checking things */

fwrite (name , 1 , sizeof(name) , lptr);
fwrite (&nNo , 1 , sizeof(nNo) , lptr);

/* checked file without waiting for it to close and
/* found nothing in the file. It's empty. Why? */

...............
...............

fclose(lptr);
Oct 31 '06 #1
3 1477
John Smith wrote:
I try to write some data to a file. After some data had supposedly
been written to the file, I opened the file before the program
closed it and found it empty. What's wrong with my code? Thanks.
From the standard

7.9.3.3 When a stream is unbuffered, characters are intended to appear from the
source or at the destination as soon as possible. Otherwise characters may be
accumulated and transmitted to or from the host environment as a block. When a
stream is fully buffered, characters are intended to be transmitted to or from
the host environment as a block when a buffer is filled. When a stream is line
buffered, characters are intended to be transmitted to or from the host
environment as a block when a new-line character is encountered. Furthermore,
characters are intended to be transmitted as a block to the host environment
when a buffer is filled, when input is requested on an unbuffered stream, or
when input is requested on a line buffered stream that requires the transmission
of characters from the host environment. Support for these characteristics is
implementation-defined, and may be affected via the setbuf and setvbuf functions.
You could try fflush on the stream or closing and opening after each write.

Although the code is incomplete I would guess that there is nothing wrong with
it causing the output not to appear in the file.

It could also be possible that the operating system is also buffering the output
and wont write it until the program has exited.

Adrian
Oct 31 '06 #2
John Smith <js****@company.comwrote:
I try to write some data to a file. After some data had supposedly
been written to the file, I opened the file before the program
closed it and found it empty. What's wrong with my code? Thanks.
FILE *lptr
char *name;
int nNo;
.............
.............
lptr=fopen("myfile","w")
..............
..............
/* deep in a function */
sprintf(buffer, "%s\r\n",name);
printf("No is %d. Name is %s\n",nNo,buffer);
Why first copy everything to 'buffer' when printf() can print out
what 'name' is pointing directly?
/* they printed OK on screen, my way of checking things */
fwrite (name , 1 , sizeof(name) , lptr);
If 'name' is the variable you defined above then this call is probably
not what you wanted to do - it will write out as many chars as a char
pointer is long on your system, not the string. Replace 'sizeof(name)'
by 'strlen(name)' to write out the string (or use fprintf()).
fwrite (&nNo , 1 , sizeof(nNo) , lptr);
/* checked file without waiting for it to close and
/* found nothing in the file. It's empty. Why? */
As Adrian pointed out most standard C output functions store output
in their internal buffers before writing them to disk (at least as
long as there's enough place left in the buffers) in order to speed
up execution. To make sure things have got written to the disk either
call fflush() on the FILE pointer or 'unbuffer' it using setbuf() or
setvbuf() (or close the file).
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
Oct 31 '06 #3
On Tue, 31 Oct 2006 00:44:05 UTC, John Smith <js****@company.com>
wrote:
I try to write some data to a file. After some data had supposedly
been written to the file, I opened the file before the program
closed it and found it empty. What's wrong with my code? Thanks.
No, you've written something to the stream related to the file.
Without flushing the output stream no data is written to disk in your
sample.

You have to do excessive error checking to catch any possible error
(looks like you have not). Opening a file already open for write to
read it will fail on most systems.

You have NOT posted any relevant information we need to help you.
Rework your program to any possible error checking in it - and then,
when this will compile and run without any error come back.

--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2 Deutsch ist da!
Oct 31 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

48 posts views Thread by Joseph | last post: by
6 posts views Thread by Sebastian Kemi | last post: by
3 posts views Thread by ishekar | last post: by
2 posts views Thread by melanieab | last post: by
reply views Thread by Yunus's Group | last post: by
16 posts views Thread by Claudio Grondi | last post: by
6 posts views Thread by arne.muller | last post: by
3 posts views Thread by Barry Flynn | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.