Recent

Author Topic: Problem with TStringList?  (Read 3147 times)

osddeitf

  • New Member
  • *
  • Posts: 22
Problem with TStringList?
« on: July 06, 2015, 05:32:20 am »
Hi, i have an code that divide an ansistring into multiple and add it to TStringList:
Code: [Select]
procedure getString(var List: TStringList; const FileName: string);
var
  Memory: TMemoryStream;
  text: string;
begin
  Memory:= TMemoryStream.Create;
  Memory.LoadFromFile(FileName);
  setlength(text, Memory.Size);
  Memory.ReadBuffer(pointer(text)^, length(text));
  Memory.Free;

  while (pos(chr(124), text) <> 0) and (length(text) <> 0) do  //chr(124) = '|'
  begin
    List.Add(copy(text, 1, pos(chr(124), text) - 2));
    text:= copy(text, pos(chr(124), text) + 1, length(text) - pos(chr(124), text));
  end;
And my files is:
  <str1>|<str2>|<str3>|...|||||~   
While run i have an error. I guess that have some error in my code. Thanks for any help.

bytebites

  • Hero Member
  • *****
  • Posts: 633
Re: Problem with TStringList?
« Reply #1 on: July 06, 2015, 08:24:29 am »
Code: [Select]
procedure getString(var List: TStringList; const FileName: string);
var
  Memory: TMemoryStream;
  text:string;
begin
 List:=TStringList.Create;
 Memory:=TMemoryStream.create;
 Memory.LoadFromFile(FileName);
 setlength(text, Memory.Size);
 Memory.ReadBuffer(pointer(text)^, length(text));
 Memory.free;
 List.Delimiter:=chr(124);
 List.DelimitedText:=text;
end;

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Problem with TStringList?
« Reply #2 on: July 06, 2015, 09:23:30 am »
Or more simply:

Code: [Select]
procedure getString(var List: TStringList; const FileName: string);
begin
 List:=TStringList.Create;
 List.Delimiter:=chr(124);
 List.LoadFromFile(FileName);
end;

I suggest designing this kind of routine as a function:
Code: [Select]
function GetStringsFromFile(const FileName): TStringList;

This has the slight advantage that it is more obvious that you are creating a stringlist that needs to be freed at some point subsequently.

osddeitf

  • New Member
  • *
  • Posts: 22
Re: Problem with TStringList?
« Reply #3 on: July 06, 2015, 12:13:04 pm »
Thanks bytebites and howardpc, your codes worked.

wp

  • Hero Member
  • *****
  • Posts: 11858
Re: Problem with TStringList?
« Reply #4 on: July 06, 2015, 12:20:07 pm »
You may want to add "List.strictDelimiter := true" to howardpc's code if the strings contain spaces.

jc99

  • Hero Member
  • *****
  • Posts: 553
    • My private Site
Re: Problem with TStringList?
« Reply #5 on: July 06, 2015, 09:50:54 pm »
Only a small corrrection:
Code: [Select]
function GetStringsFromFile(const FileName:string): TStringList; // type of Filename was missing   
This has the slight advantage that it is more obvious that you are creating a stringlist that needs to be freed at some point subsequently.
In my opinion creating an object here and having to release it there, has always the posibility of memleaks.
I try to do it like this
Code: [Select]
   lStringList := TStringList.Create;
   try
      GetStringsFromFile(FileName,lStringList);
      [...]
   finally
      FreeAndNil(lStringList);
   end;
So you can be save of mem-Leaks
The GetStringsFromFile has to look like this.
Code: [Select]
procedure GetStringsFromFile(var List: TStringList; const FileName: string);
begin
 if not assigned(List) then
   exit;
 List.Delimiter:=chr(124);
 List.strictDelimiter := true; // Thanks WP
 List.LoadFromFile(FileName);
end;
The next advantage is that the code is not limited to TStringList only, but can also use any TStringList-descendent.
But as I said before, It's a matter of taste.
OS: Win XP x64, Win 7, Win 7 x64, Win 10, Win 10 x64, Suse Linux 13.2
Laz: 1.4 - 1.8.4, 2.0
https://github.com/joecare99/public
'~|    /''
,_|oe \_,are
If you want to do something for the environment: Twitter: #reduceCO2 or
https://www.betterplace.me/klimawandel-stoppen-co-ueber-preis-reduzieren

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: Problem with TStringList?
« Reply #6 on: July 08, 2015, 08:05:03 pm »
I would tackle the memoryleak problem with howardpc's code. Just change it to function:

Code: [Select]
function getString(const FileName: string): TStringList;
begin
  result:=TStringList.Create;
  result.Delimiter:=chr(124);
  result.LoadFromFile(FileName);
end;

 

TinyPortal © 2005-2018