0

I am using binary file for storing measured data of some product. The product was the only one type before, now I have to be able to save/load more types of product.

I'm going to save some type descriptor at the beginning of the file, 1 Byte should be absolutely enough, there will be only a few types (2, maybe 3 or 4 in future).

The problem is, that I still need to be able load old binary files without this descriptor. Here is my old code with commentary where I want to check presence of descriptor and afterwards make a decision about product type like this:

  1. No descriptor -> old product
  2. Descriptor = xxx -> new product xxx

Is it possible to save descriptor in such a format? I guess the calling reader.PeekChar() is only one possibility because of not moving to next bytes, but I'm not sure how to use it in this case.

     BinaryReader reader;
     using (reader = new BinaryReader(File.Open(header.path, FileMode.Open, FileAccess.Read)))
     {
         // ...
         // check presence of product type descriptor
         // make a decision of type
         // ...

         DateTime measTime = DateTime.FromOADate(reader.ReadDouble());
         double diameter = reader.ReadDouble();
         double plusToler = reader.ReadDouble();
         double minusToler = reader.ReadDouble();
     }
4
  • 2
    Your source stream is an ordinary file and thus seekable, right? If so, what's the problem with (temporarily) "moving to next bytes"? Commented Jan 8, 2016 at 12:55
  • Oh you are right. Temporarily seeking did not occur to me. Commented Jan 8, 2016 at 13:00
  • I'd use a dummy descriptor on the old products and discard it when the dummy value is detected. That would be much more efficient than traversing back and forth in the file. Both methods will work Commented Jan 8, 2016 at 13:23
  • @nicomp: But the problem is, that I need to be able to load also old files without descriptor. Seeking solved this problem. It is not so loss of efficiency, I'm using 4 bytes for sure, so I read 4 bytes and then seek back, that's all. Commented Jan 8, 2016 at 13:38

1 Answer 1

1

The problem if I understand correclty is that you don't know whether you're reading the type descriptor (new file) or the first data value (old file).

One simple way to solve this is to choose a different file extension for the new files - but depending on your situation this may not be an option.

If not, you could prefix the product descriptor with a magic value which never (or unlikely) appears in the old file format. Something as simple as the ASCII characters "TYP" followed by the product descriptor byte would be quite unlikely to appear in the old file format (which starts with a double value).

You could even choose to serialize double.MinValue as your magic value, as DateTime.FromOADate can only read

a value between negative 657435.0 through positive 2958465.99999999

That would completely rule out falsely identifying an old file as a new one.

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah, I ended up with the second solution you suggest. I save char[4] sequence in similar format like you mentioned as this sequence can't appear in old data. Then using reader.ReadChars(4), parse answer, and finally reader.Seek(-4, SeekOrigin.Current) in case of descriptor wasn't found,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.