【问题标题】:How to read int array from binary file c#如何从二进制文件c#中读取int数组
【发布时间】:2013-02-09 13:34:47
【问题描述】:

我有以下代码:

LZW lzw = new LZW();
int[] a = lzw.Encode(imageBytes);

FileStream fs = new FileStream("image-text-16.txt", FileMode.Append);
BinaryWriter w = new BinaryWriter(fs);

for (int i = 0; i < a.Length; i++)
{
   w.Write(a[i]);

}

w.Close();
fs.Close();

如何从文件中读取数组元素?我尝试了几种方法。例如,我将数组的长度写入文件,并尝试读取数字。但是,我失败了。

注意。我需要获取 int 数组。

【问题讨论】:

标签: c# bytearray binaryfiles


【解决方案1】:

你在找这个吗:

var bytes = File.ReadAllBytes(@"yourpathtofile");

或者更多类似的东西:

        using (var filestream = File.Open(@"C:\apps\test.txt", FileMode.Open))
        using (var binaryStream = new BinaryReader(filestream))
        {
            for (var i = 0; i < arraysize; i++)
            {
                Console.WriteLine(binaryStream.ReadInt32());
            }
        }

或者,一个带有单元测试的小例子:

用整数创建一个二进制文件...

    [Test]
    public void WriteToBinaryFile()
    {
        var ints = new[] {1, 2, 3, 4, 5, 6, 7};

        using (var filestream = File.Create(@"c:\apps\test.bin"))
        using (var binarystream = new BinaryWriter(filestream))
        {
            foreach (var i in ints)
            {
                binarystream.Write(i);
            }
        }
    }

还有一个从二进制文件读取的小示例测试

    [Test]
    public void ReadFromBinaryFile()
    {
        // Approach one
        using (var filestream = File.Open(@"C:\apps\test.bin", FileMode.Open))
        using (var binaryStream = new BinaryReader(filestream))
        {
            var pos = 0;
            var length = (int)binaryStream.BaseStream.Length;
            while (pos < length)
            {
                var integerFromFile = binaryStream.ReadInt32();
                pos += sizeof(int);
            }
        }
    }

另一种读取二进制文件的方法

    [Test]
    public void ReadFromBinaryFile2()
    {
        // Approach two
        using (var filestream = File.Open(@"C:\apps\test.bin", FileMode.Open))
        using (var binaryStream = new BinaryReader(filestream))
        {
            while (binaryStream.PeekChar() != -1)
            {
                var integerFromFile = binaryStream.ReadInt32();
            }
        }
    }

【讨论】:

  • +1,没注意到,他正在处理二进制文件(由于txt 扩展名和int 在问题标题中)
  • @bas,我对数组大小(arraysize)的定义有问题。
  • @Denis,我正在看电影自动取款机,希望更新对您有所帮助。 Stefan 还提供了第三个选项来遍历二进制文件 fs.Length / sizeof(int)
【解决方案2】:

我会反过来说。唯一的问题是您在阅读之前不知道尺寸,所以先计算一下。哦,我会使用“使用”来确保正确处理(和关闭)东西:

        int[] ll;
        using (FileStream fs = File.OpenRead("image-text-16.txt"))
        {
            int numberEntries = fs.Length / sizeof(int);
            using (BinaryReader br = new BinaryReader(fs))
            {
                ll = new int[numberEntries];
                for (int i = 0; i < numberEntries; ++i)
                {
                    ll[i] = br.ReadInt32();
                }
            }
        }
        // ll is the result

我不太明白你为什么要从 LZW 写一个 int[],但我想这是有原因的......

【讨论】:

  • 他正在写“int[] from LZW”,因为当你声明一个数组时,你必须包含数组的数据类型,这意味着数组的成员将保存数组的数据类型。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-20
  • 1970-01-01
  • 2021-09-07
  • 2012-12-28
  • 2013-09-09
  • 1970-01-01
相关资源
最近更新 更多