Recent

Author Topic: Check file  (Read 334 times)

AlphaInc.

  • New Member
  • *
  • Posts: 13
Check file
« on: September 21, 2020, 05:22:57 pm »
Hello everybody,

I Want to implement a procedure to check if a pre-defined file contains a certain string. The text-file has four lines and the procedure should just check for line number 1. If its possible i would like to use a format similiar to xml:

<text1>helloworld</text1>
<text2>text</text2>
...

Or something like this:
text1:     helloworld
text2:     text
...

But if it's too complicated I also can write lines:
hellworld
text
...

Then, if line number 1 does contain the string it should do task 1, if not task 2. This is my code so far.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FileCheck;
  2. begin
  3.     if //NEED HELP HERE := 'helloworld' then
  4.     Speedbutton1.Captaion := 'Yes'
  5.     else
  6.     Speedbutton1.Captaion := 'No'
  7. end;

I only need help in how to access the file and check for the string. Thank you for your help...

lucamar

  • Hero Member
  • *****
  • Posts: 3084
Re: Check file
« Reply #1 on: September 21, 2020, 05:50:38 pm »
There is a basic explanation about how to handle files in the wiki's Pascal Introduction, in the page: Files.

Basically what you do is:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FileCheck;
  2. var
  3.   MyFile: Text; { or TextFile }
  4.   ALine: String;
  5. begin
  6.   AssignFile(MyFile, 'MyFileName.ext');
  7.   Reset(MyFile);
  8.   ReadLn(MyFile, ALine);
  9.   CloseFile(MyFile);
  10.   if MyLine = '<text1>helloworld</text1>' then
  11.     Speedbutton1.Caption := 'Yes'
  12.   else
  13.     Speedbutton1.Caption := 'No';
  14. end;

You should, of course, add the habitual error checking, etc. to make it robust; that's just an example!

Note also that, unlike in the page cited above, this code uses AssignFile and CloseFile; those are convenience aliases to System.Assign and System.Close declared in the LCL because those two words are used there as methods of most objects and would clash otherwise.
« Last Edit: September 21, 2020, 05:54:35 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.8/FPC 3.0.4 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

AlphaInc.

  • New Member
  • *
  • Posts: 13
Re: Check file
« Reply #2 on: September 21, 2020, 06:08:39 pm »
First of all, thank you for your comment.

I get one error and I don't know where and how to define it. It says that the Identifier "MyLine" is not found.

winni

  • Hero Member
  • *****
  • Posts: 1908
Re: Check file
« Reply #3 on: September 21, 2020, 06:26:34 pm »
Hello!

This is a typo by Lucamar.

His variable is named "Aline".
So you should change the line

if MyLine ....

into

if Aline ....

Winni

wp

  • Hero Member
  • *****
  • Posts: 7640
Re: Check file
« Reply #4 on: September 21, 2020, 06:26:50 pm »
When you look at the error message carefully you'll find the line number in which the error occurs. It is the line with "if MyLine = ...'. Now when you look at the variable declaration block ("var" section) you'll see that there is no "MyLine", but a string "ALine". So, lucamar probably only did a typo, and the correct statement should be "if ALine = '<text1><helloworld></text1>' then".
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

AlphaInc.

  • New Member
  • *
  • Posts: 13
Re: Check file
« Reply #5 on: September 21, 2020, 06:31:42 pm »
Yeah, it was a typo, thank you all.

I'm a little afraid to ask, I don't want to annoy anyone but another question - I tried to use this code to read the second line but this didn't work. What do I need to change in the code to do this ?

winni

  • Hero Member
  • *****
  • Posts: 1908
Re: Check file
« Reply #6 on: September 21, 2020, 06:52:26 pm »
Hi!

The dumb version goes this way:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FileCheck;
  2. var
  3.   MyFile: Text; { or TextFile }
  4.   Line1, lin2: String;
  5. begin
  6.   AssignFile(MyFile, 'MyFileName.ext');
  7.   Reset(MyFile);
  8.   ReadLn(MyFile, Line1);
  9.   ReadLn(MyFile, Line2);
  10.   CloseFile(MyFile);
  11.   showMessage ('Line1 '+line1+LineEnding +
  12.                            'Line2 '+line2);
  13. end;
  14.  
  15.  

The better way is to use a StringList:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FileCheck;
  2. var st : TStringList;
  3.  
  4. begin
  5. St := TStringList.create;
  6. St.loadFromFile('MyFileName.ext');
  7.  
  8. showMessage ('Line Zero '+st[0]);
  9. showMessage ('Line One '+st[1]);
  10. st.free;
  11. end;
  12.  

Winni

AlphaInc.

  • New Member
  • *
  • Posts: 13
Re: Check file
« Reply #7 on: September 21, 2020, 07:40:07 pm »
Thank you. That worked perfect.

 

TinyPortal © 2005-2018