3

The console displays 0,0,0,0 when I am expecting 0,1,2,3.

This is a modified version of: https://msdn.microsoft.com/en-us/library/system.io.binarywriter(v=vs.110).aspx

using System;
using System.IO;

namespace testingfilereadwrite
{    
class Program
{
    const string FileName = "TestFile.dat";       
static void Main()
    {
        WriteDefaultValues();
        DisplayValues();
        Console.ReadKey();
    }        
public static void WriteDefaultValues()
    {
        using (BinaryWriter writer = new BinaryWriter(File.Open(FileName, FileMode.Create)))
        {
            writer.Write(0);
            writer.Write(1);
            writer.Write(2);
            writer.Write(3);
        }
    }

    public static void DisplayValues()
    {
        byte byte1;
        byte byte2;
        byte byte3;
        byte byte4;

        if (File.Exists(FileName))
        {
            using (BinaryReader reader = new BinaryReader(File.Open(FileName, FileMode.Open)))
            {
                byte1 = reader.ReadByte();
                byte2 = reader.ReadByte();
                byte3 = reader.ReadByte();
                byte4 = reader.ReadByte();
            }

            Console.WriteLine(byte1);
            Console.WriteLine(byte2);
            Console.WriteLine(byte3);
            Console.WriteLine(byte4);
        }
    }
}`

Why does it display only 0? How can I get it to display what I need? Also, why does it work when I use something other than byte, like string or int.

2 Answers 2

6

Because you are writing integers to the file and the first 4 bytes are 0.

    public static void WriteDefaultValues()
    {
        using (BinaryWriter writer = new BinaryWriter(File.Open(FileName, FileMode.Create)))
        {
            writer.Write((byte)0);
            writer.Write((byte)1);
            writer.Write((byte)2);
            writer.Write((byte)3);
        }
    }

Try that.

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

2 Comments

Thank you your solution works but Shar1er80 told me why it was messing up in the first place.
He explained it better, but I also say why (above the code). The .Write is an overload and the default numeric type in .NET is an integer (unless it has a decimal point, then its a double). Each write in your original code wrote 4 bytes, so instead of having a 4-byte file you had a 16 byte one. If you get these problems in the future, try opening the file in a Hex editor.
5

When you write to your data file:

    public static void WriteDefaultValues()
    {
        using (BinaryWriter writer = new BinaryWriter(File.Open(FileName, FileMode.Create)))
        {
            writer.Write(0);
            writer.Write(1);
            writer.Write(2);
            writer.Write(3);
        }
    }

You're actually writing an integer (4 bytes) to your file.

So when you read, you read the first 4 bytes of your file, which is your zero that you wrote (0x00, 0x00, 0x00, 0x00). In your write method, cast your values to a byte and you'll get your expected results.

    public static void WriteDefaultValues()
    {
        using (BinaryWriter writer = new BinaryWriter(File.Open(FileName, FileMode.Create)))
        {
            writer.Write((byte)0);
            writer.Write((byte)1);
            writer.Write((byte)2);
            writer.Write((byte)3);
        }
    }

1 Comment

Ohhh. Thanks so much. I have much to learn in the ways of file read/write and data types.

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.