Recent

Author Topic: tstringlist handling files delphi  (Read 2688 times)

Mark12345

  • New Member
  • *
  • Posts: 17
tstringlist handling files delphi
« on: April 10, 2021, 05:33:50 pm »
is there any chance that i can read the file without showing the content with tstringlist
i want to read from the file a random item of the file without showing the content
the purpose of this is reading an answer let´s say 'a' is and answer so it reads 'a' and then i check with (if else if else) if it was correct


Code: Pascal  [Select][+][-]
  1. var
  2.   sl:  TStringList;
  3.   s,answ: string;
  4.   ix: Integer;
  5.   i:integer;
  6. begin
  7. randomize;
  8. memo1.lines.clear;
  9.   sl := TStringList.Create;
  10.   try
  11.     sl.Add('a');
  12.     sl.Add('bb');
  13.     sl.Add('ccc');
  14.     sl.Add('dddd');
  15.     sl.Add('eeeee');
  16.     sl.Add('ffffff');
  17.     sl.Add('ggggggg');
  18.     sl.Add('hhhhhhhh');
  19.     sl.SaveToFile('testudo.txt')
  20.   finally
  21.     sl.Free;
  22.   end;
  23.   sl := TStringList.Create;
  24.   try
  25.     sl.LoadFromFile('testudo.txt');
  26.  
  27.    
  28.    
  29.      begin
  30.      ix := Random(8);
  31.      s := sl[ix];
  32.      
  33.      Memo1.Lines.Add(s); ///instead of this read from the file
  34.      s without showing it
  35.      
  36.      answ:=edit1.text;
  37.      if answ=s then
  38.       begin
  39.       showmessage('correct');
  40.       end;
  41.  
  42.  
  43.      end;
  44.   finally
  45.     sl.Free;
  46.   end;
  47. end;
  48.  

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: tstringlist handling files delphi
« Reply #1 on: April 10, 2021, 05:43:19 pm »
If you mean if you can random access read a text file indexed by line number, the answer is no.

wp

  • Hero Member
  • *****
  • Posts: 11854
Re: tstringlist handling files delphi
« Reply #2 on: April 10, 2021, 05:52:44 pm »
is there any chance that i can read the file without showing the content with tstringlist
When you read a file into a TStringList you do not "show" it, the items in the stringlist are not displayed anywhere unless you assign the stringlist to a visual control such as a memo or listbox or others.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: tstringlist handling files delphi
« Reply #3 on: April 10, 2021, 05:55:03 pm »
Your question is a little difficult to understand in view of your code: you don't want to show the string but you add it to a memo; then you want to replace that by a read from the file, but you did already read the file (into the TStringList).

What is it that you're trying to accomplish? As things are now, you don't need to show s by adding it to the memo. In fact, you don't even need it (or the memo, for that matter): just compare the answer to sl[ix] and you're done! :o
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Handoko

  • Hero Member
  • *****
  • Posts: 5130
  • My goal: build my own game engine using Lazarus
Re: tstringlist handling files delphi
« Reply #4 on: April 10, 2021, 07:02:03 pm »
Delete the TMemo component, problem solved.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: tstringlist handling files delphi
« Reply #5 on: April 10, 2021, 07:38:28 pm »
Delete the TMemo component, problem solved.

OP: What everybody is trying to say is that you're confusing a TStringList- which is purely organisational- with TMemo which is a visual component.

A TMemo has a TStringList built into it as the structure in which text may be manipulated under program control. But there's lots of other ways of using it.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Mark12345

  • New Member
  • *
  • Posts: 17
Re: tstringlist handling files delphi
« Reply #6 on: April 11, 2021, 05:24:42 am »
i didn´t think that was easy, thank you guys(solved)

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1111
  • Professional amateur ;-P
Re: tstringlist handling files delphi
« Reply #7 on: April 11, 2021, 02:00:01 pm »
Hey Mark,

This is the second time that you come to the forum with the same question.
If I wasn't so much sleep deprived I would look for the thread where I gave you and example project for what you wanted.

@All:
What the OP wants is to implement the sequence in a game/quiz where there is a question selected at random from a pool of questions.
Then you show the the question in the TMemo.

I think there's an impenetrable language barrier here and the example code is not helping at all.

I got to the point where I gave him an example where:
- A text file contains a list of questions
- That text file is loaded from Form.OnCreate into a private member FQuestions: TStringList.
- If you pressed a button, a random question would be added to the TMemo
- That question would be deleted from FQuestions, so no repetitions

So, I thought the OP had enough to chew on to continue their game.
I'm baffled that she/he came back with the same near exact code example that I already had a go at...

So my conclusion: impenetrable language barrier.

i didn´t think that was easy, thank you guys(solved)

Taking this at face value, hopefully this is the last we're gonna hear from her/him for a while.

@Mark12345
If English is so difficult for you this forum has 4 more languages you can try and post on:

Cheers,
Gus
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1111
  • Professional amateur ;-P
Re: tstringlist handling files delphi
« Reply #8 on: April 11, 2021, 03:45:03 pm »
Hey All,

I finally found the post: https://forum.lazarus.freepascal.org/index.php/topic,53974.0.html

In the midst of it all I switched to Spanish because of something I saw, can't remember now what exactly.

But the most intriguing thing is that it all went nowhere because the OP is using Delphi 7 and my project is for Lazarus.
He complained that the project gave an error when opening and then when I mentioned that it wouldn't open unless he modified some of the files... crickets...

He could at least open the *.pas file and have a look at it no?

Let's see what happens next.

Cheers,
Gus

EDIT: Read the thread to the end and it's definitely homework, since he mentioned it's from school and he was obligated to use arrays.
« Last Edit: April 11, 2021, 03:49:13 pm by Gustavo 'Gus' Carreno »
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: tstringlist handling files delphi
« Reply #9 on: April 11, 2021, 04:07:36 pm »
EDIT: Read the thread to the end and it's definitely homework, since he mentioned it's from school and he was obligated to use arrays.

I normally make a point of reminding people that their teacher will be reading this forum to find out who's asking for help- and who isn't bright enough to interpret the advice they're given.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1111
  • Professional amateur ;-P
Re: tstringlist handling files delphi
« Reply #10 on: April 11, 2021, 04:13:16 pm »
Hey MarkMLI,

I normally make a point of reminding people that their teacher will be reading this forum to find out who's asking for help- and who isn't bright enough to interpret the advice they're given.

ROTFL !!!! That is effin brilliant!! That completely crushes it :D

And on the matter of plagiarism, I'm gonna use this every time I see hint of homework, if you don't mind?

Thanks for the good laugh!!

Cheers,
Gus
« Last Edit: April 11, 2021, 04:23:20 pm by Gustavo 'Gus' Carreno »
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: tstringlist handling files delphi
« Reply #11 on: April 11, 2021, 04:41:22 pm »
And on the matter of plagiarism, I'm gonna use this every time I see hint of homework, if you don't mind?

Feel free :-)

I do sometimes feel I ougt to update my sig with the standard "tell us what OS and version of compiler etc. you've got, you lackwit!" and I suppose that could be added to it.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1111
  • Professional amateur ;-P
Re: tstringlist handling files delphi
« Reply #12 on: April 11, 2021, 04:56:18 pm »
Hey MarkMLI, (In this thread I need the MLI to not bundle you and OP)

Feel free :-)

Thanks !!!

I do sometimes feel I ougt to update my sig with the standard "tell us what OS and version of compiler etc. you've got, you lackwit!" and I suppose that could be added to it.

May I be allowed to pile on the hate? I could not agree more !!

Cheers,
Gus
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: tstringlist handling files delphi
« Reply #13 on: April 11, 2021, 05:58:15 pm »
Pile it on! I'm always happy to help, but rather less so when the advice is ignored and the question asked again.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1111
  • Professional amateur ;-P
Re: tstringlist handling files delphi
« Reply #14 on: April 11, 2021, 06:30:57 pm »
Hey MarkMLI,

Pile it on! I'm always happy to help, but rather less so when the advice is ignored and the question asked again.

- I've linked to the other thread with my project. (I actually posted 2 versions for him, before I understood Delphi and Homework, how gullible was I).
- I've summarized his predicament.
- We have enough posts after I recognized the fellah with hints that he's been spotted.

So let's hope he gets the message. Nonetheless, let's be vigilant for his screen name.

Cheers,
Gus
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

 

TinyPortal © 2005-2018