I’m creating a temp file to store a huge matrix that can’t fit in memory. I’d like to write the temp file to disk first so I can seek to a position in the file with a stream object. I already know the exact size I need.
What’s the easiest way to write, say, 2 gigabytes of zeros?
I tried writing it one byte at a time and that’s really slow. I also tried writing byte array chuncks and that’s a lot faster but I must suck at the math cause I can’t get the final size to be what I expect.
The BinaryWriter has a method Seek that allows you to set an arbitrary position on a file also if this position is outside the end of the file.
If you write a simple byte at this position, the OS will resize the file to allow the write at the requested position.