Efficient online search and large file replacement

There are some standard tools for this, but I need a simple graphical interface to help some users (on windows). They open a dialog box with an open file and select a file for processing.

The file will be an XML file. The file will contain (within the first few lines) a text line that should be deleted or replaced with a space (no matter which).

The problem is that the XML file is several gigabytes in size, but a fixed search and replace string will occur within the first 4 thousand or so.

What is the best way to rewrite the search string and keep it in place without requiring you to read the whole amount into memory and write too much to disk?

+3
source share
2 answers

Obviously, the replacement is with a space, so the file size as a whole does not change, this is the best choice here, otherwise you must transfer the entire file for updating to disk.

If this were for a Unix environment, I would consider using the mmap()matching part of the beginning of the file in RAM to match, and then edit it in place and execute it.

This fragment shows how to use the Win32 equivalent CreateFileMapping().

+1
source

You can easily write your own tool. If this is at the very beginning, then any brute force approach will work. Just keep scanning until you find it.

, . - , , . . . , .

0

Source: https://habr.com/ru/post/1706033/


All Articles