Editing file without overwriting

I am writing library for text editor; and i, obviously, will need to open files. I can simply write file content to a variable, edit variable and overwrite existing file content. But what if you work with big files? Overwriting whole file won't be good.
So the question is: which are other ways of fast editing files in rust?

You can always seek to a position and write some data there.

let mut fh = OpenOptions::new().read(true).write(true).open(filepath)?;

Whether that is the right thing to do in your editor, or if you rather need atomic writes, I can't tell.

1 Like

One question you need to consider is whether new text has been written midway through the file. Appends can be done easily enough by seeking to the end of the file and writing, but insertions require you to shuffle everything after the insertion point down.

Most text editors I know about do indeed read the entire file into memory (using a data structure called a "rope") and write it back to file upon saving. Granted, they do not work well with gigantic files, but human-readable text files are usually not gigantic, so this shouldn't usually be a concern.

If the files are not very large, then overwriting whole files atomically is actually the best option. By atomically I mean:

  1. Create a new file with a temporary name
  2. Write entire contents to the new file
  3. Rename the new file to the old file's name, which replaces the old file

If you have so much data that overwriting entire file is too costly, then you should probably be using sqlite.


Seems like a good solution, but can i see your uses? Like from where you take SeekFrom, etc.

Certainly. I copied those lines from a tool I made that can losslessly rotate mp4 videos without rewriting the whole file. It only changes 36 bytes (the translation matrix) somewhere in the file.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.