Recent

Author Topic: LoadFromFile  (Read 5617 times)

Zath

  • Sr. Member
  • ****
  • Posts: 391
LoadFromFile
« on: October 17, 2018, 02:36:25 am »
The following will read a file and list the lines.
How can I populate a string array rather than fill a memo box ?
Does it read the file in one go or line by line ?
Thanks

Code: Pascal  [Select][+][-]
  1. FileListPath:='C:\Box\FileList.txt';
  2. Memo1.Lines.LoadFromFile(FileListPath);
  3.  
« Last Edit: October 17, 2018, 02:38:03 am by Zath »

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: LoadFromFile
« Reply #1 on: October 17, 2018, 03:09:40 am »
How can I populate a string array rather than fill a memo box ?
Does it read the file in one go or line by line ?
Thanks

As for the first point, TMemo.Lines is just a (kind of) TStringList so you can replace one for the other very easily--p.e. LoadFromFile is a method of TStringList. If you really want a simple array of strings, you should investigate TTextReader and descendants (TStreamReader and TFileReader) to load from/save to file.

I don't know about the second point but you can just look at the implementation to see how it is done.

HTH

Edit: some typos and clarified some points.
« Last Edit: October 17, 2018, 03:28:35 am by lucamar »
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: 5131
  • My goal: build my own game engine using Lazarus
Re: LoadFromFile
« Reply #2 on: October 17, 2018, 12:33:16 pm »
TStringList and TStringList.LoadFromFile are very handy, it can save your a lot of time for writing code. You can see my example here:

https://forum.lazarus.freepascal.org/index.php/topic,42900.msg299648.html#msg299648

 

TinyPortal © 2005-2018