If you use a platform that supports it, you can use mmap (). File pagination is also possible, but remember to keep the buffer as large as possible to reduce I / O overhead and be careful between the borders of two pages (suppose the line matches but is separated by the page border)
As an alternative, I suggest you create some kind of index and use the index to limit your search. KMP search is not particularly effective. This, of course, depends on the nature of your file, its creation, etc.
source
share