2
  1. Suppose there is only one single double value written into a file in binary format. How can I read that value using C# or Java?

  2. If I have to find a double value from a huge binary file, what techniques should I use to find that?

Alex Aza
  • 76,499
  • 26
  • 155
  • 134
Barun
  • 1,885
  • 3
  • 27
  • 47

6 Answers6

10

Double is 8 bytes. To read single double from binary file you can use BitConverter class:

var fileContent = File.ReadAllBytes("C:\\1.bin");
double value = BitConverter.ToDouble(fileContent, 0);

If you need to read double from the middle of the file, replace 0 with the byte offset.

If you don't know the offset, you can't possibly tell that certain value in the byte array is double, integer or string.

Another approach is:

using (var fileStream = File.OpenRead("C:\\1.bin"))
using (var binaryReader = new BinaryReader(fileStream))
{
    // fileStream.Seek(0, SeekOrigin.Begin); // uncomment this line and set offset if the double is in the middle of the file
    var value = binaryReader.ReadDouble();
}

The second approach is better for big files, as it does not load whole file content to the memory.

Alex Aza
  • 76,499
  • 26
  • 155
  • 134
2

You can use the BinaryReader class.

double value;
using( Stream stream = File.OpenRead(fileName) )
using( BinaryReader reader = new BinaryReader(stream) )
{
    value = reader.ReadDouble();
}

For the second point, if you know the offset, simply use the Stream.Seek method.

Sven
  • 21,903
  • 4
  • 56
  • 63
1

It seems that we would need to know how the double value was encoded in the file before we could go about finding it.

Timothy Lee Russell
  • 3,719
  • 1
  • 35
  • 43
  • are there different encoding? – Barun Jun 21 '11 at 03:56
  • Perhaps this value is written using C++ using default encoding. – Barun Jun 21 '11 at 03:57
  • Sure, IEEE 754 is the standard encoding but the double value could be encoded in another form. How much do you know about the source file? As @AlexAza pointed out, unless you know the offset, you'll have a hard time knowing whether you're getting a "correct" double. If you don't know anything about the format of the file, I don't know if finding arbitrary double values is possible. – Timothy Lee Russell Jun 21 '11 at 04:07
  • Well its a .wt1 file. Some custom database. (Winpilot thermal database) – Barun Jun 21 '11 at 04:11
  • Unfortunately, I think to get useful information out of the file, you're going to need to know how it was encoded as well as the format (i.e. how the encoded information is organized in the file). – Timothy Lee Russell Jun 21 '11 at 04:20
  • I would suggest contacting the WinPilot people and ask. If you're gracious, they will likely send you the format which will make your job a lot easier. – Timothy Lee Russell Jun 21 '11 at 04:57
0

1)

        double theDouble;
        using (Stream sr = new FileStream(@"C:\delme.dat", FileMode.Open, FileAccess.Read))
        {
            byte[] buffer = new byte[8];
            sr.Read(buffer, 0, 8);

            theDouble = BitConverter.ToDouble(buffer, 0);
        }

2) You can't.

Petar Ivanov
  • 91,536
  • 11
  • 82
  • 95
0

Here's how to read (and write for testing purposes) a double:

    // Write Double
    FileStream FS = new FileStream(@"C:\Test.bin", FileMode.Create);
    BinaryWriter BW = new BinaryWriter(FS);

    double Data = 123.456;

    BW.Write(Data);
    BW.Close();

    // Read Double
    FS = new FileStream(@"C:\Test.bin", FileMode.Open);
    BinaryReader BR = new BinaryReader(FS);

    Data = BR.ReadDouble();
    BR.Close();

Getting it out of a large file depends on how the data is laid out in the file.

Steve Wellens
  • 20,506
  • 2
  • 28
  • 69
  • 1
    You should use `using` blocks instead of just calling `Close`. – Sven Jun 21 '11 at 03:57
  • 'Teaching' code is different than production code. In production code, I handle ALL errors and plan for ALL contingencies. In teaching code, I keep it as simple as possible to convey concepts. – Steve Wellens Jun 21 '11 at 14:54
  • Although error handling is another matter, I strongly believe following certain best practices in code samples like this is very important, so you don't teach people bad habits. The `using` keyword definitely falls under that category. Plus, imho `using` actually makes the code simpler in this case. – Sven Jun 21 '11 at 14:59
0
using (FileStream filestream = new FileStream(filename, FileMode.Open))
using (BinaryReader reader = new BinaryReader(filestream))
{
    float x = reader.ReadSingle();
}
I. J. Kennedy
  • 24,725
  • 16
  • 62
  • 87