Fast and simple find and replace string in large binary file lazarus / 21-General ---------------------------------------------------------------------- Cyclone · 2021-11-02 10:49 Hi! This is my first post on this forum, despite the fact that I have been reading it for many years. I have a question that I cannot figure out on my own, so I decided to ask here. I have several hundred of binary files up to 20 MB in size (task for the Windows platform). The first 100 bytes of some files contain ANSI-string up to 10 characters long. I need to go through all the files, determine if the file contains this line, and if it does, then replace it with another line (with the same length), while not changing any other bytes from the file, and overwrite it back to the disk. After several hours of reading forums and programming, I have not found a simple and fast solution. First, I loaded the full file in memory (may be not the fastest way, I didn't figure out how to work only with first, let say, 100 bytes of file). I tried opening binaries in a TStringList, but after saving back to disk, LF bytes were changed to a CR LF (determined as line end delimiter). Tried TMemoryStream, but couldn't find an efficient way to find and replace a string. I haven’t figured it out to the end with TStringStream. Please advise what is the easiest and fastest way to solve this problem. Thank you in advance. ---------------------------------------------------------------------- marcov · 2021-11-02 10:56 IS the string to replace the same length as the existing one? ---------------------------------------------------------------------- Cyclone · 2021-11-02 10:59 Quote from: marcov on November 02, 2021, 11:56:32 amIS the string to replace the same length as the existing one? Yes, it is. ---------------------------------------------------------------------- marcov · 2021-11-02 11:03 Then you can just use (from memory search in docs for the "file" filetype and blockread/write for more details) Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var f : file;   buf : array[0..4095] of byte;  byteswritten,  bytesread : integer;byteswrittenbegin  assignfile(f,'filename.something.txt');  reset(f,1);  blockread(f,buf,sizeof(buf),bytesread);  // inspect and modify buf  if needs modification then   begin     seek(f,0);     blockwrite(f,buf,bytesread,byteswritten);   end; closefile(f);end; ---------------------------------------------------------------------- Bart · 2021-11-02 14:30 In a 20 MB file, one blockread won't do, and if you are unlucky you read the first part of the string in block X and the rest in block X+1. Bart ---------------------------------------------------------------------- marcov · 2021-11-02 14:38 Quote from: Bart on November 02, 2021, 03:30:43 pmIn a 20 MB file, one blockread won't do, and if you are unlucky you read the first part of the string in block X and the rest in block X+1. In the original message was that the string was in the first 100 bytes. And if the replacement is same size, you don't have to read/write the whole file, you can just do an in place update with seek. ---------------------------------------------------------------------- Bart · 2021-11-02 15:22 Quote from: marcov on November 02, 2021, 03:38:45 pmIn the original message was that the string was in the first 100 bytes. Missed that. Sorry. Bart ---------------------------------------------------------------------- Cyclone · 2021-11-02 17:39 Quote from: marcov on November 02, 2021, 12:03:40 pmThen you can just use (from memory search in docs for the "file" filetype and blockread/write for more details) Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var f : file;   buf : array[0..4095] of byte;  byteswritten,  bytesread : integer;byteswrittenbegin  assignfile(f,'filename.something.txt');  reset(f,1);  blockread(f,buf,sizeof(buf),bytesread);  // inspect and modify buf  if needs modification then   begin     seek(f,0);     blockwrite(f,buf,bytesread,byteswritten);   end; closefile(f);end; Many thanks! I tried your code and it works fine. But I didn't get why the buffer size is 4096 bytes? Any specific reason for that (program speed, e.g. disk sector size?)? I need only the first 100 bytes. In blockread doc example buffer size is 2048. I also tried 100 bytes long buffer, and it also works fine. ---------------------------------------------------------------------- marcov · 2021-11-02 17:57 Yeah, sector size.  100 bytes will work fine. ---------------------------------------------------------------------- MarkMLl · 2021-11-02 19:43 Or if the first few bytes really do have a well-understood format define an appropriate record, read/write precisely that number of bytes, and leave the compiler to do the hard work and optimisation. MarkMLl ---------------------------------------------------------------------- Warfley · 2021-11-02 20:46 If you need very high performance, you should consider mapping the file into memory (On Windows: Link, on Unix: Link). Then simply search through the memory region for the pattern (e.g. using a pointer) and override the memory.