Binary Reading of Bytes Returning only One Value. C#

十年热恋 提交于 2019-12-12 15:28:41

问题


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.


回答1:


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.




回答2:


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);
        }
    }


来源:https://stackoverflow.com/questions/29755324/binary-reading-of-bytes-returning-only-one-value-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!