Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing to the middle of the file (without overwriting data)

In windows is it possible through an API to write to the middle of a file without overwriting any data and without having to rewrite everything after that?

If it's possible then I believe it will obviously fragment the file; how many times can I do it before it becomes a serious problem?

If it's not possible what approach/workaround is usually taken? Re-writing everything after the insertion point becomes prohibitive really quickly with big (ie, gigabytes) files.


Note: I can't avoid having to write to the middle. Think of the application as a text editor for huge files where the user types stuff and then saves. I also can't split the files in several smaller ones.

like image 896
Thomas Bonini Avatar asked Mar 07 '10 21:03

Thomas Bonini


People also ask

How to write to file without overwriting C?

Yes, it is possible. By opening it with r+ you open it for 'reading and writing' (while w opens it for writing freshly). t -> Opens the file as text, specifically parsing \n as \r\n under Windows. That's better.

How write data to file without overwriting in C++?

you need to set the output in "append" mode.... ofstream fout("c:/My Documents/TextFile. txt", ios::app); in this example whatever you want to add to a file will be inserted after the last item in a file thus no items will be overwritten.....

Does fprintf append or overwrite?

You're overwriting the content of the file every time because you're using mode w to write the file, which re-writes the file from the beginning. If you simply want to append your data to the end of the file, you should use mode a like this: fptr=fopen("program.

Does write () overwrite?

To overwrite a file, to write new content into a file, we have to open our file in “w” mode, which is the write mode. It will delete the existing content from a file first; then, we can write new content and save it.


1 Answers

I'm unaware of any way to do this if the interim result you need is a flat file that can be used by other applications other than the editor. If you want a flat file to be produced, you will have to update it from the change point to the end of file, since it's really just a sequential file.

But the italics are there for good reason. If you can control the file format, you have some options. Some versions of MS Word had a quick-save feature where they didn't rewrite the entire document, rather they appended a delta record to the end of the file. Then, when re-reading the file, it applied all the deltas in order so that what you ended up with was the right file. This obviously won't work if the saved file has to be usable immediately to another application that doesn't understand the file format.

What I'm proposing there is to not store the file as text. Use an intermediate form that you can efficiently edit and save, then have a step which converts that to a usable text file infrequently (e.g., on editor exit). That way, the user can save as much as they want but the time-expensive operation won't have as much of an impact.

Beyond that, there are some other possibilities.

Memory-mapping (rather than loading) the file may provide efficiences which would speed things up. You'd probably still have to rewrite to the end of the file but it would be happening at a lower level in the OS.

If the primary reason you want fast save is to start letting the user keep working (rather than having the file available to another application), you could farm the save operation out to a separate thread and return control to the user immediately. Then you would need synchronisation between the two threads to prevent the user modifying data yet to be saved to disk.

like image 194
paxdiablo Avatar answered Nov 15 '22 19:11

paxdiablo