Re: fwrite() misses writing a byte
- From: Keith Thompson <kst-u@xxxxxxx>
- Date: Wed, 03 Oct 2007 13:39:01 -0700
hemant.gaur@xxxxxxxxx writes:
I have an application which writes huge number of bytes into the
binary files which is just some marshalled data.
int len = Data.size(); //arrary size
for (int i = 0; i < len; ++i)
fwrite(&Data[i], 1, 1, f);
now after running this for long time and pushing millions of bytes, It
once misses writing the last byte of fData. Then the further push of
bytes is again correct. As i am not using the return value for the
fwrite I think there is some error which could be due to buffer
overrun in the stream. This is hard to reproduce and happening on the
Solaris.
I presume that Data[i] is a single byte (type char, signed char, or
unsigned char). If it isn't, then you're just writing the first byte
of Data[i], which could be the high-order byte, or the low-order byte,
or even something else.
If you really want to write a single byte at a time, you might as well
use fputc() rather than fwrite(). Either way, you *must* check the
value returned by the function to determine whether it succeeded or
failed. On most systems, a failure will also result in the setting of
errno; you can then use strerror() or perror() to construct a
meaningful error message.
You should also decide whether you're using C or C++. Your use of
both ``Data.size()'' and ``Data[i]'' suggests that you're using
operator overloading, which C++ supports but C doesn't. In C++, you
might prefer to use some other mechanism for output, though C++
supports the C stdio interface as well (ask in comp.lang.c++).
When asking for help, you should post a complete compilable program if
possible. If that's not practical, you should at least show us the
declaration of Data.
--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <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."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
.
- Follow-Ups:
- Re: fwrite() misses writing a byte
- From: Army1987
- Re: fwrite() misses writing a byte
- References:
- fwrite() misses writing a byte
- From: hemant . gaur
- fwrite() misses writing a byte
- Prev by Date: Re: Should we broaden the topicality of this group?
- Next by Date: Re: C (functional programming) VS C++ (object oriented programming)
- Previous by thread: Re: fwrite() misses writing a byte
- Next by thread: Re: fwrite() misses writing a byte
- Index(es):
Relevant Pages
|
|