简体   繁体   中英

Trying to overwrite data in the middle of a file without affecting the beginning of the file

I have an algorithm that calculates components (bytes) of a vector out of order. I'm storing the vector in a file, since when I run this for real the vector will be millions of bytes long. But as a simplified example I'd like to start with an empty file:

-1 -1 -1 -1 -1 -1 -1 -1

Then my algorithm will fill in two values at a time:

-1 -1 4 7 -1 -1 -1 -1

then

-1 -1 4 7 -1 -1 3 9

etc. until all the values have been filled in. Is this possible to do? I've tried using random access files, but when you use the seek() method it fills in 0s for all values before the ones I want to edit, deleting any previous calculations my algorithm has done. Since I need this program to work on large sequences of numbers, pulling the whole file into memory is not an option. Any ideas?

Not really an answer just a suggestion here. Sorry i can't comment on statuses yet

Open up the file Store those values in memory small chunks at a time then just write to a different file while clearing the memory and stuff on the way

Most file systems are not truly random access, so from a practical standpoint it's difficult or impossible to modify the middle of a file without reading the whole file from and writing it back to disk. Probably what you will need to do is read in the file one section at a time, modify that section appropriately, then write it back out to a temporary file. After you're done, you can replace the original file with the temporary file.

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