Recent

Author Topic: Value of string disappears  (Read 7121 times)

Davidous

  • Full Member
  • ***
  • Posts: 107
Value of string disappears
« on: August 16, 2018, 11:02:32 am »
Hello,

I'm new to Lazarus, I did a lot of programming in Delphi 7. Now I started to write my programs from Delphi to Lazarus, but I faced a very strange thing.

My program loads a text file to a stringlist. Then from this stringlist I need some lines as a string. I give these values to different strings. Then my program goes on and after a few more lines in the program when I check my string values I can see, that they are all ???... I suppose that means that they are empty. It is a really simple code, I don't know what could cause the problem. Please help me!

 oD:=TOpenDialog.Create(self);
 oD.InitialDir := GetCurrentDir;
 oD.Title:='Kérem válassza ki a mérési fájlt!';
 oD.Filter:='Txt fájlok|*.txt';
 oD.Execute;

 list:=TStringList.Create;
 list.LoadFromFile(oD.FileName);

 tr:=AnsiMidStr(list[0],22,length(list[0])-21);
 d:=AnsiMidStr(list[10],15,length(list[10])-14);
 fn:=ExtractFileName(oD.FileName);
 dl:=TStringList.Create;
 dl.Delimiter:='.';
 dl.DelimitedText:=d;

For w:=0 To 20 Do
Begin
If AnsiLeftStr(fn,4)='left' Then
Begin
If (AnsiMidStr(list[w],19,length(list[w])-18)='vorwärts') And (AnsiMidStr(list[w+1],20,length(list[w+1])-19)='steigend') Then
Begin
oldbj:='b';
Break;
end
Else If (AnsiMidStr(list[w],19,length(list[w])-18)='rückwärts') And (AnsiMidStr(list[w+1],20,length(list[w+1])-19)='fallend') Then
Begin
oldbj:='b';
Break;
end
Else If (AnsiMidStr(list[w],19,length(list[w])-18)='vorwärts') And (AnsiMidStr(list[w+1],20,length(list[w+1])-19)='fallend') Then
Begin
oldbj:='j';
Break;
end
Else If (AnsiMidStr(list[w],19,length(list[w])-18)='rückwärts') And (AnsiMidStr(list[w+1],20,length(list[w+1])-19)='steigend') Then
Begin
oldbj:='j';
Break;
end;
end
Else If AnsiLeftStr(fn,4)='righ' Then
Begin
If (AnsiMidStr(list[w],19,length(list[w])-18)='vorwärts') And (AnsiMidStr(list[w+1],20,length(list[w+1])-19)='steigend') Then
Begin
oldbj:='j';
Break;
end
Else If (AnsiMidStr(list[w],19,length(list[w])-18)='rückwärts') And (AnsiMidStr(list[w+1],20,length(list[w+1])-19)='fallend') Then
Begin
oldbj:='j';
Break;
end
Else If (AnsiMidStr(list[w],19,length(list[w])-18)='vorwärts') And (AnsiMidStr(list[w+1],20,length(list[w+1])-19)='fallend') Then
Begin
oldbj:='b';
Break;
end
Else If (AnsiMidStr(list[w],19,length(list[w])-18)='rückwärts') And (AnsiMidStr(list[w+1],20,length(list[w+1])-19)='steigend') Then
Begin
oldbj:='b';
Break;
end;
end;
end;

Thaddy

  • Hero Member
  • *****
  • Posts: 14371
  • Sensorship about opinions does not belong here.
Re: Value of string disappears
« Reply #1 on: August 16, 2018, 11:39:43 am »
Well, compile in mode delphi. Your code works, but I had to do a lot to write a compilable version.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Davidous

  • Full Member
  • ***
  • Posts: 107
Re: Value of string disappears
« Reply #2 on: August 16, 2018, 12:26:03 pm »
I'm sorry, but I still can't get it to work. I set the compiler mode to delphi:

{$mode delphi}

but the result is the same. Strings are not stored for some reason. :(

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: Value of string disappears
« Reply #3 on: August 16, 2018, 12:26:14 pm »
What OS do you use and what is format of the text file (UTF8 or so)?
This may be related to different LineEnding in Windows and Linux.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

Davidous

  • Full Member
  • ***
  • Posts: 107
Re: Value of string disappears
« Reply #4 on: August 16, 2018, 01:05:37 pm »
I use win10 but I also tested it on win7 with the same results.
The text file is UTF-8.

HeavyUser

  • Sr. Member
  • ****
  • Posts: 397
Re: Value of string disappears
« Reply #5 on: August 16, 2018, 01:25:21 pm »
two main problems
1)make sure that Ansileftstr,ansimidstr etc are returning the correct string.
2)change the comparison to use comparetext(string1,string2) = 0.

There are other points of failure as well, for example AnsiMidStr(list[w],19,length(list[w])-18), what happens if the string has an extra space or two at the end?

Davidous

  • Full Member
  • ***
  • Posts: 107
Re: Value of string disappears
« Reply #6 on: August 16, 2018, 02:12:00 pm »
Extra characters cannot be at the end, because the txt files I use are always generated by another program. This code worked without an error in delphi 7 but in Lazarus it doesn't. When I give the value to the string it stores it. I checked it during runtime, when I paused the proccess, but a few steps later all stored strings disappear.

Davidous

  • Full Member
  • ***
  • Posts: 107
Re: Value of string disappears
« Reply #7 on: August 16, 2018, 02:27:34 pm »
I attached some pictures. On the first one you can see, that everything seems to be all right, string value is what it is supposed to be. A few steps later I changes, you can see that on the second picture. (That try-except is only for stopping the program at the right moment, it is not part of the program.)

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: Value of string disappears
« Reply #8 on: August 16, 2018, 02:59:14 pm »
There are breakpoints for stopping program at the right moment.
The fact that hovering over identifier fn shows ??? in tooltip does not mean that content of string is lost. It rather means that code is out of scope.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: Value of string disappears
« Reply #9 on: August 16, 2018, 06:08:05 pm »
Please attach a compilable sample program with the text-file it reads.
Also state what the expected output should be.

Bart

Davidous

  • Full Member
  • ***
  • Posts: 107
Re: Value of string disappears
« Reply #10 on: August 17, 2018, 07:26:28 am »
Hello Bart!

I zipped my project, it is attached. I attached also one txt file it reads.
The output shold be a message with the text: 'A helyes oldal: b'.
Right now the message only shows 'A helyes oldal: '.

balazsszekely

  • Guest
Re: Value of string disappears
« Reply #11 on: August 17, 2018, 08:05:11 am »
@Davidous

The encoding of the txt file is wrong(attachment1), if I fix it(attachment2) you program displays the desired output. Download the modified txt and run a few tests yourself. By the way your code formatting is horrible(saláta). Please take it as a constructive criticism. It should look more like this:
Code: Pascal  [Select][+][-]
  1.  For w:=0 To 20 Do
  2.   Begin
  3.     If AnsiLeftStr(fn,4)='left' Then
  4.     Begin
  5.       If (AnsiMidStr(list[w],19,length(list[w])-18)='vorwärts') And (AnsiMidStr(list[w+1],20,length(list[w+1])-19)='steigend') Then
  6.       Begin
  7.         oldbj:='b';
  8.         Break;
  9.       end
  10.       Else If (AnsiMidStr(list[w],19,length(list[w])-18)='rückwärts') And (AnsiMidStr(list[w+1],20,length(list[w+1])-19)='fallend') Then
  11.       Begin
  12.         oldbj:='b';
  13.         Break;
  14.       end
  15.       Else If (AnsiMidStr(list[w],19,length(list[w])-18)='vorwärts') And (AnsiMidStr(list[w+1],20,length(list[w+1])-19)='fallend') Then
  16.       Begin
  17.         oldbj:='j';
  18.         Break;
  19.       end
  20.       Else If (AnsiMidStr(list[w],19,length(list[w])-18)='rückwärts') And (AnsiMidStr(list[w+1],20,length(list[w+1])-19)='steigend') Then
  21.       Begin
  22.         oldbj:='j';
  23.         Break;
  24.       end;
  25.     end
  26.     Else If AnsiLeftStr(fn,4)='righ' Then
  27.     Begin
  28.       If (AnsiMidStr(list[w],19,length(list[w])-18)='vorwärts') And (AnsiMidStr(list[w+1],20,length(list[w+1])-19)='steigend') Then
  29.       Begin
  30.         oldbj:='j';
  31.         Break;
  32.       end
  33.       Else If (AnsiMidStr(list[w],19,length(list[w])-18)='rückwärts') And (AnsiMidStr(list[w+1],20,length(list[w+1])-19)='fallend') Then
  34.       Begin
  35.         oldbj:='j';
  36.         Break;
  37.       end
  38.       Else If (AnsiMidStr(list[w],19,length(list[w])-18)='vorwärts') And (AnsiMidStr(list[w+1],20,length(list[w+1])-19)='fallend') Then
  39.       Begin
  40.         oldbj:='b';
  41.         Break;
  42.       end
  43.       Else If (AnsiMidStr(list[w],19,length(list[w])-18)='rückwärts') And (AnsiMidStr(list[w+1],20,length(list[w+1])-19)='steigend') Then
  44.       Begin
  45.         oldbj:='b';
  46.         Break;
  47.     end;
  48.   end;


mangakissa

  • Hero Member
  • *****
  • Posts: 1131
Re: Value of string disappears
« Reply #12 on: August 17, 2018, 09:05:34 am »
Nice project. Especially for a school assignment.
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

Davidous

  • Full Member
  • ***
  • Posts: 107
Re: Value of string disappears
« Reply #13 on: August 17, 2018, 11:44:15 am »
Thank you very much for your help! :)

mangakissa

  • Hero Member
  • *****
  • Posts: 1131
Re: Value of string disappears
« Reply #14 on: August 17, 2018, 04:26:40 pm »
I couldn't help it to make a class for (not all subjects are uses, but it gives an example how to work with).

Works in FPC end delphi. Using FPC remove the contnrs in uses

(rename the txt file to pas)
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

 

TinyPortal © 2005-2018