简体   繁体   中英

Why is StreamReader hitting EndOfStream before BaseStream does?

Disclaimer: I'm bad at I/O

I've got a gigantic compressed text file that I'm trying to iterate through, line-by-line.

using (var stream = File.Open(filename, FileMode.Open))
using (var bz2 = new BZip2InputStream(stream))
using (var reader = new StreamReader(bz2))
{
   while (!reader.EndOfStream)
   {
      string line = reader.ReadLine();
      // etc

However, I'm reaching EndOfStream at line 40 and I have no idea why. Help!

I would replace StreamReader by an BinaryReader. You may find the same code here<\/a>

    using System;
    using System.IO;

    class Program
    {
        static void Main()
        {
            R();
        }

        static void R()
        {
            // 1.
            using (BinaryReader b = new BinaryReader(
                File.Open("file.bin", FileMode.Open)))
            {
                // 2.
                // Position and length variables.
                int pos = 0;
                // 2A.
                // Use BaseStream.
                int length = (int)b.BaseStream.Length;
                while (pos < length)
                {
                    // 3.
                    // Read integer.
                    int v = b.ReadInt32();
                    Console.WriteLine(v);

                    // 4.
                    // Advance our position variable.
                    pos += sizeof(int);
                }
            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM