Telling an empty binary file from a "full" one



I have a binary file used to store the values of variables in order to
use them again. I easily know whether the file exists or not, but the
problem is, in case the program has been earlier interupted before it
could write the variables to the file, the file is gonna be empty, and
then it's gonna load a load of crap into variables, which i want to
avoid.

That file is always 36 bytes big (it contains 4 double-precision floats
and one integer) and i'd like to be able to test whether it is 36 bytes
long or not, but it seems like quite a big problem to get to do it in a
portable way.

I thought that using fseek and ftell could work if the end of file
could be told but i read that "Setting the file position indicator to
end-of-file, as with fseek(file, 0, SEEK_END), has undefined behavior
for a binary stream (because of possible trailing null characters)"

My file has lots of zero bytes in it, so I guess it means it can't tell
then end of file reliably, right? I'd just like to know how I can, in a
reliable and portable way, tell the size of my binary file, and if not,
tell whether my file is empty or not

.



Relevant Pages

  • Re: cd torito
    ... but anyhow it is not important and leave default (empty). ... Best regards, ... > The binary file should be 32 KB in size or less. ... > offset zero of the ISO image. ...
    (microsoft.public.windowsxp.embedded)
  • Re: Telling an empty binary file from a "full" one
    ... >I have a binary file used to store the values of variables in order to ... >then it's gonna load a load of crap into variables, ... size would be to attempt to fread) 37 bytes, ... I would, though, make the point that you have emphasized portability ...
    (comp.lang.c)
  • Re: size_type, pos_type, etc.
    ... Should I convert it to off_type, which is a true integer? ... same binary file, should I store the pos_type value returned by ... functions, especially when mixing variables of different types, e.g., if I ...
    (comp.lang.cpp)
  • Storing/processing binary file input help needed
    ... I need to read a binary file and store it into a buffer in memory (system ... I'm confused with how to store the binary file into memory. ...
    (comp.lang.c)
  • Re: Storing/processing binary file input help needed
    ... >> I need to read a binary file and store it into a buffer in memory ... >> binary files before so I'm confused with how to store the binary file ... >> declare a DWORD buffer since that's what the function is taking as ...
    (comp.lang.c)

Loading