Link to home
Start Free TrialLog in
Avatar of sprinken
sprinken

asked on

Searching for text in a file

How can I know if some text can be find in a text file? I tried loading the file in a memo and then searching for it but when I have many and large files to look into it will take too much time.

thanks
Avatar of Jase-Coder
Jase-Coder

you could put your code for loading the file and reading the file into a thread. This might make the performance better.
I am not sure if you can search a file without open and reading from it.
Use the TFindDialog component.  The help file describe it use pretty well.
There is no way to search through data except one item at a time, from the first likely place to the last. The fastest method I can think of is to seek for just one character - just the first in the seek string - and when that is found, check if the next few characters in the file match the rest of the seek string.
You may also find it helpful to load as large an amount of data as possible at one time, if there is a lot to be searched through. Disk access is a timewaster, so loading small buffers as needed slows things down. If you're working with many small files, as long as they are all together in sequence on the disk, you could load several into one buffer.
Lastly, if speed is really imortant, then consider using delphi's Assembler to get the repetetive tasks, like the search loop, done more rapidly. This way there is nothing unnecessary going on behind the scenes.
ASKER CERTIFIED SOLUTION
Avatar of Pierre Cornelius
Pierre Cornelius
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sprinken

ASKER

Thanks Pierre! Sorry for being late
Glad I could help