Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

How do I write a data file in C#?

I have figured out how to read/write a text file in C# but if I wanted to wright ints or doubles, I would have to use a data file.

in java I would use DataInputStream/ DataOutputStream to write data to a file.

1 Answer

Relevance
  • 1 decade ago
    Favorite Answer

    If you want to write straight primitive data types out to files in binary you can use BinaryReader and BinaryWriter.

    FileStream fs = File.Create("C:\\test.dat");

    BinaryWriter br = new BinaryWriter(fs);

    double dblToWrite = 14.3;

    br.Write(dblToWrite);

    br.Close()

    And then when you want to read your value back in, you would do this:

    FileStream fs = File.Open("C:\\test.dat", FileMode.Open);

    BinaryReader bRead = new BinaryReader(fs);

    double dblReadFromFile = bRead.ReadDouble();

    bRead.Close();

Still have questions? Get your answers by asking now.