How to implement line by line search in Delphi file? if possible with an example.
Closed due to the fact that the essence of the question is incomprehensible by the participants of Streletz , HamSter , aleksandr barakin , Kirill Stoianov , Denis Oct 8 '16 at 16:07 .
Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .
- Well, first, you need to understand what you are looking for; you can process it with regulars. then you need to cache the entire file. in this buffer, you'll be looking for. Next, you need to decide on the search algorithm. How to decide on the aggression, implement it and everything. - Alex.B
- Probably can read the file line by line. Then read the line, look for it. Given written above. - Vladimir Martyanov
|
1 answer
var LF: TextFile; LStr: string; begin AssignFile(LF, 'myfile.txt'); // Открываем файл Reset(LF); try while not Eof(LF) do // Пока в файле не кончатся строки, повторяем begin Readln(LF, LStr); // Читаем следующую строку из файла if (LStr = 'abc') // <<-- Условие полного совпадения строки or (Pos('x', LStr) > 0) then // <<-- Условия наличия символа в строке ShowMessage('Found'); end; finally CloseFile(LF); // Закрываем файл end; end; - No, not AssignFile! :) What will you do with files that are not in ANSI encoding? - kami
- one@kami You will not believe it - I will indicate the third parameter
function AssignFile(var F: File; FileName: String; [CodePage: Word]): Integer;- Anton Shchyrov - Great! Plus. The only pity is that this third parameter appeared already in the XE versions ... Understands almost everything, including UTF-8 with and without BOM. But I didn’t want to work with UTF-16BE ... We need to rethink our attitude to this heritage of the turbo-belts :) - kami
- The problem here is that the line-by-line reading of the file is very slow. For labs it will do - for professional software it will not stand the competition. Lines can be converted and compared, but in this way, if you re-read a file of 50 meters, it will take quite a long time. Readln certainly has a buffer (not 30-40 bytes read, several kilobytes), but here it is still small. It is better to read the stream in portions and search in it. - Albert Fomin
|