Re: fwrite() misses writing a byte



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



Relevant Pages

  • Re: Access violation in free()
    ... Better: char *CopyString ... writing. ... looking for a new word processor. ...
    (comp.lang.c)
  • Re: Is there a standard way to write an 8 bit byte?
    ... > I would like to write completely platform-independent code if at all ... char is only guaranteed to be at least 8 bits. ... writing a char into a file in binary mode would result in 9 bits being ... There is no cast iron guarantee in the C standard that this will produce the ...
    (comp.lang.c)
  • Re: generics and arrays and multi-class collections
    ... with the variable name, that is, a pointer is not char* p, but char ... you get pretty tired of writing.field. ... And then there's the lack of nice basic string manipulation. ... Everything is so bloody low-level. ...
    (comp.lang.java.programmer)
  • Re: 100% CPU Util on Simple Update Query
    ... I actually got it to fail on the statement shown. ... I was writing into a char 25 field. ... I suspect an indexing ...
    (microsoft.public.sqlserver.programming)
  • java.io.FileOutputStream misbehavior
    ... is it right that when writing a file, ... one or two byte get written per char? ... For example you save a String with about 40kbyte into a file. ... When putting a single umlaut in the String, ...
    (comp.lang.java.help)