Recent

Author Topic: problem with WideCompareText and txt base  (Read 4300 times)

mrkaban

  • Jr. Member
  • **
  • Posts: 59
    • КонтинентСвободы
problem with WideCompareText and txt base
« on: August 28, 2016, 09:51:09 am »
Hello, I warn you right away that I'm a novice. I apologize if I create a theme is not so where necessary. Also, I apologize in advance for my English!

Help please understand. Found WideCompareText, and with its help, I try to compare program names to the database in a text file.

The main issue in these two lines

Code: Pascal  [Select][+][-]
  1.  Result:= WideCompareText( widestring(s), widestring(d) );
  2.  if Result=0 then begin

Here I am trying to compare two variables that reads lines from a text file, and therefore the comparison result is assigned to the variable Result. I read that 0 means a coincidence. And then I tried to make a condition that, if matched, then write to the table.

The problem is that with Result = (0), it does not write anything to the table (screenshot attached), if you put 1 or -1, then it just all in a row or record or randomly.


Below is a full procedure code

Code: Pascal  [Select][+][-]
  1. procedure TfMian.Button1Click(Sender: TObject);
  2. var
  3.    MyList: TStringList;
  4.    MyRegistry: TRegistry;
  5.    i: Integer;
  6.    Str: string;
  7.    install: TextFile;
  8.    TempStr: String;
  9. // Copy-paste to populate the table
  10.      s: WideString;   // Read from the install
  11.      d: WideString;   // Read from the NameProgramm in d
  12.      f: string;   // Read from the Rasprost
  13.      g: string;   // Read from the License
  14.      h: string;  // Read from the Stoimost
  15.      j: string;  // Read from the Zamena
  16.      k: integer; // to fill these
  17.      Result: integer; // Compare
  18.      for1: integer;
  19.  
  20. begin
  21. Cursor:= crHourGlass;
  22. bSearch.Visible:=False;
  23. Button1.Visible:=False;
  24. // Get the list of installed programs
  25.      MyRegistry:=TRegistry.Create;
  26.      MyList:=TStringList.Create;
  27.      AssignFile(install, UTF8ToSys('install.txt'));
  28.      ReWrite(install);
  29.          with MyRegistry do
  30.             begin
  31.             RootKey:=HKEY_LOCAL_MACHINE;
  32.             if OpenKey('Software\Microsoft\Windows\CurrentVersion\Uninstall',
  33.             False)=True then GetKeyNames(MyList);   // Get the key variable mylist
  34.             TempStr := MyList.Text;           // Convert to string
  35.             Writeln(install, TempStr); // Write to file
  36.             CloseKey;
  37.               end;
  38.          CloseFile(install);
  39.           try
  40. // Connect text files for further work with them
  41.    AssignFile(install, 'install.txt');
  42.       Reset(install);
  43.       AssignFile(NameProgramm, 'NameProgramm.txt');
  44.       Reset(NameProgramm);
  45.       AssignFile(Rasprost, 'Rasprost.txt');
  46.       Reset(Rasprost);
  47.       AssignFile(License, 'License.txt');
  48.       Reset(License);
  49.       AssignFile(Stoimost, 'Stoimost.txt');
  50.       Reset(Stoimost);
  51.       AssignFile(Zamena, 'Zamena.txt');
  52.       Reset(Zamena);
  53. // Do until the end of the file:
  54.        while not Eof(install) do begin
  55.  
  56.             Cursor:= crHourGlass;
  57.             Readln(install, s); // Read in with the next line
  58.             Readln(NameProgramm, d);
  59.             Readln(Rasprost, f); // Read in and the next row
  60.             Readln(License, g);
  61.             Readln(Stoimost, h);
  62.             Readln(Zamena, j);
  63.  
  64.             Result:= WideCompareText( widestring(s), widestring(d) );
  65.                if Result=0 then begin
  66. // Further it is necessary to record that found
  67.                  StringGrid1.RowCount:= StringGrid1.RowCount + 1;
  68.                  k:= StringGrid1.RowCount;
  69.                  SetLength(d, (k * 15));
  70.                  StringGrid1.Cells[0, StringGrid1.RowCount-1]:= UTF8ToSys(s);
  71.                  StringGrid1.Cells[1, StringGrid1.RowCount-1]:= UTF8ToSys(f);
  72.                  StringGrid1.Cells[2, StringGrid1.RowCount-1]:= UTF8ToSys(g);
  73.                  StringGrid1.Cells[3, StringGrid1.RowCount-1]:= UTF8ToSys(h);
  74.                  StringGrid1.Cells[4, StringGrid1.RowCount-1]:= UTF8ToSys(j);
  75.               end;                                    
  76.  
  77.               end;
  78.    finally
  79.       CloseFile(install);
  80.       CloseFile(NameProgramm);
  81.       CloseFile(Rasprost);
  82.       CloseFile(License);
  83.       CloseFile(Stoimost);
  84.       CloseFile(Zamena);
  85.       Cursor:= crDefault;
  86.    end;  //try
  87. end;
Possible problem in the database, it is in the form of text files. If not hard, please see. Here is a link to the archive with the project. The message could not be attached.

If you assign values directly to variables, then the function works as it should.

PS: The project is open source. Indicates which installed by a licensed programs are distributed.

Thaddy

  • Hero Member
  • *****
  • Posts: 14162
  • Probably until I exterminate Putin.
Re: problem with WideCompareText and txt base
« Reply #1 on: August 28, 2016, 11:45:06 am »
. I read that 0 means a coincidence.
That is not the case. The comparison works like this:
- if the strings are equal the result is 0; So remember, zero means equal. If you read something different you have been reading nonsense. Ignore.
- if the second string is longer, the result is positive (any value)
- if the second string is shorter, the result is negative (any value)

On some platforms it may return an actual index, but this is not always reliable.
« Last Edit: August 28, 2016, 11:51:29 am by Thaddy »
Specialize a type, not a var.

mrkaban

  • Jr. Member
  • **
  • Posts: 59
    • КонтинентСвободы
Re: problem with WideCompareText and txt base
« Reply #2 on: August 28, 2016, 12:10:10 pm »
. I read that 0 means a coincidence.
That is not the case. The comparison works like this:
- if the strings are equal the result is 0; So remember, zero means equal. If you read something different you have been reading nonsense. Ignore.
- if the second string is longer, the result is positive (any value)
- if the second string is shorter, the result is negative (any value)

On some platforms it may return an actual index, but this is not always reliable.

I deliberately copied from one of the names in install.txt NameProgramm.txt. Compares the string as the time of these files. And in principle, the lines should be equal in length.

In the future, I want to convert SQLite, but so far there is not enough knowledge. Yes instructions yet little about it.

mrkaban

  • Jr. Member
  • **
  • Posts: 59
    • КонтинентСвободы
Re: problem with WideCompareText and txt base
« Reply #3 on: August 28, 2016, 12:11:37 pm »
Probably base txt originally a bad idea?

Thaddy

  • Hero Member
  • *****
  • Posts: 14162
  • Probably until I exterminate Putin.
Re: problem with WideCompareText and txt base
« Reply #4 on: August 28, 2016, 12:17:15 pm »
No, that doesn't matter. Good enough for small jobs.

But understand that zero is returned if the strings are EXACTLY equal, not only in length. Except for Uppercase/Lowercase.
I would use the TStringlist.LoadFromFile and TStringList.SaveToFile methods, instead of Assign etc.
If you use a Tstringlist any way, make optimum use of it.
Specialize a type, not a var.

mrkaban

  • Jr. Member
  • **
  • Posts: 59
    • КонтинентСвободы
Re: problem with WideCompareText and txt base
« Reply #5 on: August 28, 2016, 12:26:06 pm »
No, that doesn't matter. Good enough for small jobs.

But understand that zero is returned if the strings are EXACTLY equal, not only in length. Except for Uppercase/Lowercase.
I would use the TStringlist.LoadFromFile and TStringList.SaveToFile methods, instead of Assign etc.
If you use a Tstringlist any way, make optimum use of it.
I wonder if you can give an example of the use? I can not understand how to work with these techniques.

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4458
  • I like bugs.
Re: problem with WideCompareText and txt base
« Reply #6 on: August 28, 2016, 12:48:38 pm »
Lazarus uses UTF-8 as its default encoding.
I think you should use type String and AnsiCompareText(). Less hassle. It is even Delphi compatible.
« Last Edit: August 28, 2016, 12:52:27 pm by JuhaManninen »
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

mrkaban

  • Jr. Member
  • **
  • Posts: 59
    • КонтинентСвободы
Re: problem with WideCompareText and txt base
« Reply #7 on: August 28, 2016, 12:56:53 pm »
Lazarus uses UTF-8 as its default encoding.
I think you should use type String and AnsiCompareText(). Less hassle. It is even Delphi compatible.
Did, but still does not work = (

I do not understand what went wrong

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4458
  • I like bugs.
Re: problem with WideCompareText and txt base
« Reply #8 on: August 28, 2016, 01:23:23 pm »
I do not understand what went wrong

Debug your code. It reveals all problems.
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

Bart

  • Hero Member
  • *****
  • Posts: 5265
    • Bart en Mariska's Webstek
Re: problem with WideCompareText and txt base
« Reply #9 on: August 28, 2016, 10:33:16 pm »
That is not the case. The comparison works like this:
- if the strings are equal the result is 0; So remember, zero means equal. If you read something different you have been reading nonsense. Ignore.
- if the second string is longer, the result is positive (any value)
- if the second string is shorter, the result is negative (any value)

Wrong.
WideCompareText returns:
0: strings are equal ignoring upper/lowercase
1: the first string alphabetically comes after the second one (like if you would look them up in a dictionary)
-1: the second string alpahbetically comes after the first one

Bart

 

TinyPortal © 2005-2018