Recent

Author Topic: Searching a text file.  (Read 5559 times)

metal mark

  • New Member
  • *
  • Posts: 38
Searching a text file.
« on: April 08, 2014, 02:11:57 pm »
Hi , I am a beginner, so this is probably a stupid question.
I am trying to search a text file for a combination of letters so I want to examine the file a line at a time. Using 1 to Eof is no good in my loop as its a boolean so how can I find out how many lines are in my text file so that I can do something like '1 to Line_count Do'. Been searching and searching but cant find anything on this subject.

Thanks Mark M

eric

  • Sr. Member
  • ****
  • Posts: 267
Re: Searching a text file.
« Reply #1 on: April 08, 2014, 02:30:48 pm »
I would load the file into a stringlist, and then search the stringlist line by line.

Code: [Select]
var
  MyList: TStringlist;
.
.
MyList := TStringlist.Create;
try
  MyList.LoadFromFile(path-to-filename);
  <do the searching>
finally
  MyList.Free;
end;

Roland57

  • Sr. Member
  • ****
  • Posts: 423
    • msegui.net
Re: Searching a text file.
« Reply #2 on: April 08, 2014, 06:54:54 pm »
Using 1 to Eof is no good in my loop as its a boolean so how can I find out how many lines are in my text file so that I can do something like '1 to Line_count Do'.

Here is an example.

Code: [Select]
{ ReadTextFile.pas }

program ReadTextFile;

var
  f: TextFile;
  s: string;
 
begin
  AssignFile(f, 'ReadTextFile.pas');
  Reset(f);
 
  while not EOF(f) do
  begin
    ReadLn(f, s);
    WriteLn(s);
  end;
 
  CloseFile(f);
 
  WriteLn('--');
  Write('Press the Enter key... ');
  ReadLn;
end.
My projects are on Gitlab and on Codeberg.

metal mark

  • New Member
  • *
  • Posts: 38
Re: Searching a text file.
« Reply #3 on: April 08, 2014, 09:31:17 pm »
Thank you for your suggestions. They will give me something to move forward with.
Cheers  MM

metal mark

  • New Member
  • *
  • Posts: 38
Re: Searching a text file.
« Reply #4 on: April 14, 2014, 10:01:33 pm »
Using Tstringlist did partly what I wanted but when I try to step through the Tstringlist I reach a problem. I want to test each line against a given pattern and jump out when a match is reached. so :-

mylist :=Tstringlist.create;
         mylist.LoadFromFile(Path to text file) ;
         memo1.lines := mylist;    // show text file in a memo box.
         File_counter := mylist.Count;     //Get the length of the list.

           For x := 1 to File_counter Do       //step through the list.
             Begin
               ReadLn (mylist, Item_Data_Line);   // Get a line ready for testing.

But when it reaches ReadLn it says "Cant read or write variables of this type.

Cant seem to move this on, mylist is stringlist and Item_Data_Line is a string variable so what am I doing wrong.
Your help would be appreciated.
MM

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Searching a text file.
« Reply #5 on: April 14, 2014, 10:46:13 pm »
Code: [Select]
   for x := 1 to mylist.Count do       //step through the list.
   begin
     Item_Data_Line := mylist[x];    // Get a line ready for testing.
   end; 
« Last Edit: April 14, 2014, 10:48:45 pm by typo »

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Searching a text file.
« Reply #6 on: April 14, 2014, 10:56:07 pm »
TStringList indexes are zero-based.
You need to adapt code of this sort (where "pattern" is the string constant you are looking for):
Code: [Select]
var
  mylist: TStringList;
  i, p: integer;
begin
  mylist:=TStringList.Create;
  try
    mylist.LoadFromFile(Path_to_text_file);
    Memo1.Lines.Assign(mylist);
    for i:= 0 to mylist.Count-1 do
      begin
        p:=Pos(pattern, mylist[i]);
        if (p > 0) then begin
          ShowMessageFmt('Pattern "%s" found at position %d in line %d',[pattern, p, i]);
          Break;
        end;
      end;
  finally
    mylist.Free;
  end;
end;     

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Searching a text file.
« Reply #7 on: April 14, 2014, 11:13:09 pm »
TStringList indexes are zero-based.

Oh, yes, sorry.

metal mark

  • New Member
  • *
  • Posts: 38
Re: Searching a text file.
« Reply #8 on: April 15, 2014, 01:21:36 am »
Brilliant, thank you all, I see what I am doing wrong now  :D
Cheers MM

metal mark

  • New Member
  • *
  • Posts: 38
Re: Searching a text file.
« Reply #9 on: April 15, 2014, 06:48:22 am »
Well  :(, modified it to below which I thought made far more sence now you showed me but it gives its own problem :-

mylist :=Tstringlist.create;
         mylist.LoadFromFile(Path to text file) ;
         memo1.lines := mylist;    // show text file in a memo box.
         File_counter := mylist.Count;     //Get the length of the list.

           For x := 0 to File_counter-1 Do       //step through the list.
             Begin
               Item_Data_Line:= mylist
  • ;   // Get a line ready for testing.


When I compile it , it says at the last line "Got Tstringlist expected Ansistring" mylist is a Tstringlist and Item_Data_Line is a string. Really dont understand this "expected ansistring bit, can someone please explain what is going on ?
Cheers MM
« Last Edit: April 15, 2014, 06:55:17 am by metal mark »

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1260
Re: Searching a text file.
« Reply #10 on: April 15, 2014, 07:49:03 am »
Code: [Select]
   Item_Data_Line:= mylist
This is your problem line.  Item_Data_line is a string (Ansistring).  mylist is a TStringlist.  You're trying to assigned one to the other, and they're different types, so the compiler is correctly complaining.

As shown by others in their posts above, in your code snippet the correct line should be

Code: [Select]
   Item_Data_Line:= mylist[x];
Item_data_Line is a string.   mylist[ x ] returns a string.  Everything matches, compiler is happy :-)[/s]

UPDATE:  BAH, Ignore this.  I see you are in fact using [ x ] in your code, you just didn't wrap it inside a [ code ] tag in your post, and the forum code mapped [ x ] to a bullet point. (You'll see I've added extra spaces so the the forum code doesn't get confused)

Can you post your full code, and wrap it inside [ code ] tags?  I want to see what Item_Data_Line is declared as.

Many thanks
« Last Edit: April 15, 2014, 08:21:32 am by Mike.Cornflake »
Lazarus Trunk/FPC Trunk on Windows [7, 10]
  Have you tried searching this forum or the wiki?:   http://wiki.lazarus.freepascal.org/Alternative_Main_Page
  BOOKS! (Free and otherwise): http://wiki.lazarus.freepascal.org/Pascal_and_Lazarus_Books_and_Magazines

metal mark

  • New Member
  • *
  • Posts: 38
Re: Searching a text file.
« Reply #11 on: April 16, 2014, 04:34:35 am »
Sorry, have a huge amount still to learn  %),
Code: [Select]
mylist :=Tstringlist.create;
         mylist.LoadFromFile(Path to text file) ;
         memo1.lines := mylist;    // show text file in a memo box.
         File_counter := mylist.Count;     //Get the length of the list.

           For x := 0 to File_counter-1 Do       //step through the list.
             Begin
               Item_Data_Line:= mylist (x);       // Get a line ready for testing, screws up here.
                 If s  = Item_Data_Line then begin
                         Edit9.txt := Item_Data_Line;
                     end;
[code]

Variables are :-
[code]
Mylist:Tstringlist;
Filevar: Textfile;
Item_Data_Line: String;
S: String;
File_counter : Integer;
x: Integer;
[code]
Well that didnt go wonky this time, thanks for that tip  :D
With Mylist as Tstringlist and every thing else as String except the integers I thought there would not be a problem well they do say rubbish gives rubbish out. Your advice would be much appreciated.
Kind regards MM

metal mark

  • New Member
  • *
  • Posts: 38
Re: Searching a text file.
« Reply #12 on: April 16, 2014, 04:43:13 am »
Ah, now just realised, what a dummy, I put curved brackets round x not square brackets. Just ran the program with square brackets round x and the ansistring message didnt appear and the program ran ok. I shouldn't be doing this to myself at my age, but its facinating  :D
Thanks for your help, I can move on the next mistake now, love Lazarus its a joy to work with.
Thanks all,
MM

 

TinyPortal © 2005-2018