Dear all,

im new to Delphi language.can u pls help me to do this one.
i want to read a particular file and searching for a particular word in that file..
pls assist me to do this one in delphi language

Thanx in advance..
Poornima.R

Dani AI

Generated

This post corrects and expands on 's suggestion and provides a safe, working routine for . Key fixes: Readln must be called with a target variable (it does not return a string), UpCase is a char-level helper while UpperCase (or AnsiUpperCase) is the string-level function, and the substring-advance logic must move the search window correctly (or use PosEx for overlapping matches). Also watch for file-not-found errors, large-file memory use, and file encodings (ANSI vs UTF‑8/Unicode).

A compact, reliable line-by-line routine (works in classic Delphi) follows. It counts non-overlapping, case-insensitive occurrences and closes the file in a finally block:

function CountOccurrencesInFile(const FileName, SearchStr: string): Integer;
var
  F: TextFile;
  Line, S, SearchUpper: string;
  P: Integer;
begin
  Result := 0;
  if (SearchStr = '') or not FileExists(FileName) then Exit;
  AssignFile(F, FileName);
  Reset(F);
  try
    SearchUpper := UpperCase(SearchStr);
    while not Eof(F) do
    begin
      Readln(F, Line);
      S := UpperCase(Line);
      P := Pos(SearchUpper, S);
      while P > 0 do
      begin
        Inc(Result);
        Delete(S, 1, P + Length(SearchUpper) - 1);  // advance past found text
        P := Pos(SearchUpper, S);
      end;
    end;
  finally
    CloseFile(F);
  end;
end;

Notes and variants: to count overlapping matches use PosEx (StrUtils) with a start index incremented by 1. For whole‑word matches prefer regular expressions (TRegEx.Matches with \b boundaries) if the Delphi version supports it. For very large files, keep the streaming approach above (avoid TStringList.LoadFromFile). When handling UTF‑8 or other encodings, use a stream reader with the correct TEncoding to avoid garbled text.

Member Avatar for Member #82008

The procedure below must be able to count a particular word(SearchStr) occurence in a file(FileName).

procedure SearchingInFile(Filename, SearchStr :string);
var
  SearchPos,
  FoundCount :integer;
  FileLine :string;
  SearchFile :Text;
begin
  Assign(SearchFile, Filename);
  Reset(SearchFile);
  FoundCount := 0;
  while not EOF(SearchFile) do
  begin
 // the search will be no case sensitive (using UpCase)
    FileLine := UpCase(Readln(SearchFile));
    SearchPos := Pos(UpCase(SearchStr), FileLine);
    if SearchPos > 0 then
    begin
     // looking for more SearchStr into FileLine
      while SearchPos > 0 do
      begin
        // add 1 to founded word count
        Inc(FoundCount);
       // Advance SearchPos
        SearchPos := SearchPos +Length(SearchStr).
       // remove substring before last word founded, including the word
        FileLine := Copy(FileLine, SearchPos, Length(FileLine));
       // check for a new occurence of searched word on line readed
        SearchPos := Pos(UpCase(SearchStr), FileLine);
      end;
    end;
  end;
  Close(SearchFile);
end;

Is it? :rolleyes:
p.s.: I'm not sure about UpCase function in pascal (today, I'm Delphi programmer), but I think that have some thing similar.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.