0

I have two applications that needs to pass value to each other in a fast way, and some value needs to be kept(when I restart my computer it still exist), so I need to create a file, now I know how to do with int:

    using (BinaryWriter writer = new BinaryWriter(new FileStream(@"C:\TEST", FileMode.Open)))
    {
        writer.Write(0);  //00 00 00 00
        writer.Write(1);  //01 00 00 00
        writer.Write(2);  //02 00 00 00
        writer.Write(3);  //03 00 00 00 
        writer.Write(int.MaxValue); //FF FF FF 7F
    }

    byte[] test = new byte[4];
    using (BinaryReader reader = new BinaryReader(new FileStream(@"C:\TEST", FileMode.Open)))
    {

        reader.BaseStream.Seek(8, SeekOrigin.Begin);
        reader.Read(test, 0, 4);
        Console.WriteLine(BitConverter.ToInt32(test, 0));     //2

        reader.BaseStream.Seek(16, SeekOrigin.Begin);
        reader.Read(test, 0, 4);
        Console.WriteLine(BitConverter.ToInt32(test, 0));     //2147483647    

        Console.Read();
    }

But how to do with double?

2
  • 2
    You could answer this question yourself simply by reading the relevant documentation. Any question that can be answered that way should not be asked here in the first place because you should ALWAYS have read the relevant documentation before posting a question here. Commented May 23, 2018 at 5:40
  • Also, the way you're reading the data makes no sense. You're creating a BinaryReader and not even using it. Read the documentation. Commented May 23, 2018 at 5:41

1 Answer 1

1

Its as easy as

writer.Write((double)int.MaxValue);

BinaryWriter Class

Write(Double) Writes an eight-byte floating-point value to the current stream and advances the stream position by eight bytes

As for reading

reader.ReadDouble()

BinaryReader.ReadDouble Method ()

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

Comments

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.