3

In C#, the FileStream's methods Read/Write/Seek take integer in parameter. In a previous post , I have seen a good solution to read/write files that are bigger than the virtual memory allocated to a process.

This solution works if you want to write the data from the beginning to the end. But in my case, the chunks of data I am receiving are in no particular order.

I have a code that works for files smaller than 2GB :

private void WriteChunk(byte[] data, int position, int chunkSize, int count, string path)
    {

        FileStream destination = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);
        BinaryWriter writer = new BinaryWriter(destination);
        writer.Seek((int) (position*chunkSize), SeekOrigin.Begin);
        writer.Write(data, 0, count);
        writer.Close();
    }

Is there a way I can seek and write my chunks in files bigger than 2GB?

Community
  • 1
  • 1
Alex Rose
  • 127
  • 3
  • 10

2 Answers2

4

Don't use int, use long. Seek takes a long.

You need to use long everywhere though and not just cast to int somewhere.

usr
  • 168,620
  • 35
  • 240
  • 369
  • 1
    Thanks! I saw that BinaryWriter has no overload to seek with a long, but FileStream has. – Alex Rose Jun 07 '12 at 17:03
  • MSDN docs show offset parameter of BinaryWriter.Seek as an int. Double checked in VS2010 and BinaryWriter.Seek shows first paramter as int with no overloads. How is it going to take a long then ? – user957902 Jun 07 '12 at 17:07
  • 3
    You need to seek the FileStream before opening the BinaryReader. – usr Jun 07 '12 at 17:08
2
writer.Seek((long)position*chunkSize, SeekOrigin.Begin);
Joshua
  • 40,822
  • 8
  • 72
  • 132