Writing/reading to a position in a file

hello, so i have a byte array and i want to write it to a file, and read from it eventually, i know u can use writeallbytes and readallbytes but, i want to write-to and read-from an specific position (lets say position 20 in the array for simplicity sake) without having to read, or write, the whole array, is this possible?

I have seen the filestream.write, would that be a good idea? I have never used it.

I normally use Seek() with BinaryWriter/BinaryReader for this.

3 Likes

hi, seek works perfectly thanks!

1 Like

That’s so wild… we just don’t see mentions of this sort of thing much these days!

I love it and really appreciate it. When I ported all my MS-DOS and PalmOS games to run under Unity3D (as a bunch of native C / C++ code), I did the same thing where the games prepare a huge buffer of stuff and send chunks of it back to Unity for storage, save, config, etc.

Bytes, bytes, bytes, all the way down.

This is my collection of old MS-DOS and PalmOS games I wrote:

Apple iTunes: ‎KurtMaster2D on the App Store
Google Play (including TV): https://play.google.com/store/apps/details?id=com.plbm.plbm1

External Public TestFlight Link: TestFlight - Apple

Itch.io: KurtMaster2D by Kurt Dekker

2 Likes

I was going to say the same! :grin:

There should be no need to do this these days, unless you work with gigabyte-sized files but even then probably not because append-writing in the middle of a file is either going to be slow anyway or it will fragment the hell out of that file on disk.

There should be no need to do this these days, unless you work with gigabyte-sized files but even then probably not because append-writing in the middle of a file is either going to be slow anyway or it will fragment the hell out of that file on disk.

hi, the reason i do this is because of speed, writing or reading a whole file is slow, specially since i only need to modify a couple of bytes on it at a time, altou i didn’t knew the fragmentation part u mentiont, il have to check that out thanks.

Another way i thought would be to have many smaller files, so writeallbytes/readallbytes would be much faster, altou the fragmentation would happened anyway i guess(?), since any modification to the file would create fragments as far as i seen, even deleting the file would, seems unavoidable.

Perhaps your use case could benefit from Memory Mapped Files?

Caveat: I haven’t used them in very many contexts personally so I don’t have a “feel” for them, but they seem interesting.

1 Like

hi, im a newbie in programming so i don’t think i want to introduce that level of complexity for now but, since my files are 0.6 gb each, i don’t think is necessary, its just the fact that i would be accessing them regularly, think about modifying minecraft chunks, basically that’s what im storing.

Il check that out anyways thanks.

Edit: to be clear the seek way is fast enough for what i need, its just i didn’t consider the fragmentation problem if its even a problem, i don’t know, il be doing research on it, but as far as i can see even using the writeallbytes/readallbytes should create fragments no(?)

1 Like

There’s no fragmentation you should worry about. The actual fragmentation of clusters depend on the file system and you as user have no influence on it. Reading / writing and seeking through a file does not change anything about the fragmentation the file may or may not have. In theory by using low-level HDD access one could manipulate a file cluster chain in order to insert a new cluster somewhere in the middle. However you could only ever insert a full cluster / chunk. The cluster size would depend on the formatting of the volume. The file system itself would never do anything like that. From the user’s perspective a file is just a continuous piece of memory. If it’s fragmented or not depends on the file system itself and how much memory is free.

When you are on an SSD the story is slightly different. Since every write has to rewrite the whole cluster, SSDs avoid to write the same cluster several times. That’s why when a cluster is written again, the internal system of the SSD writes it to a new place to spread the wear of the memory somewhat evenly. Though fragmentation is kinda irrelevant for SSDs as they are designed to have the data spread all over the memory space. NTFS fragmentation would not be affected when rewriting / modifying clusters.

1 Like

The actual fragmentation of clusters depend on the file system and you as user have no influence on it

hi, yes i read something similar to your answer about the subject so i don’t think i need to worry about that in my specific use case. :slightly_smiling_face:

In theory by using low-level HDD access one could manipulate a file cluster chain

yea i don’t think il be doing that anytime soon :sweat_smile: seems to low level for me, thanks for clarifying!

1 Like