Autor Beitrag
Shoppix
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 19



BeitragVerfasst: Di 22.03.05 02:07 
Hallo,

ich hoff ihr könnt mir helfen, ich habe folgendes problem:

ich will in einer Txt-Datei eine Zeile suchen zB suche Nach 'abc' wenn er es findet soll er die Zeile aus der Text-Datei löschen.

Also: TxtDatei vorher:

aaa
abb
abc
abb
aaa

txtdatei nachher:

aaa
abb
abb
aaa

ohne die lehrzeil! ich bin einfach zublöd sitze schon seit 3 tagen dran und habe alle foren durchgestöber... bidde ihr seit meine letzte hoffnung

thx
shoppix
delfiphan
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2684
Erhaltene Danke: 32



BeitragVerfasst: Di 22.03.05 02:15 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
Var
 Liste : TStringList;
 Index : Integer;
begin
 Liste := TStringList.Create;
 try
  Liste.LoadFromFile(Filename);
  Index := Liste.IndexOf('abc');
  if Index >= 0 then
  begin
   Liste.Delete(Index);
   Liste.SaveToFile(Filename);
  end else
   ShowMessage('Nicht gefunden');
 finally
  Liste.Free;
 end;
end;
retnyg
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2754

SNES, GB, GBA, CPC, A500, 486/66, P4/3.0HT: NintendOS, AmigaOS, DoS
Delphi 5, Delphi 7
BeitragVerfasst: Di 22.03.05 02:23 
mist...zu langsam ;) aber ein with wäre noch schöner gewesen

_________________
es gibt leute, die sind genetisch nicht zum programmieren geschaffen.
in der regel haben diese leute die regel...
delfiphan
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2684
Erhaltene Danke: 32



BeitragVerfasst: Di 22.03.05 02:34 
Stimmt, ist aber klarer so... Vor allem für Anfänger. Die Anzahl Zeilen wär ja gleich, bis auf die Variablendeklaration ;)
retnyg
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2754

SNES, GB, GBA, CPC, A500, 486/66, P4/3.0HT: NintendOS, AmigaOS, DoS
Delphi 5, Delphi 7
BeitragVerfasst: Di 22.03.05 02:37 
die anzahl der zeilen schon, aber nicht die performance...

edit: siehe mh-nexus.de/optimizingger.htm

_________________
es gibt leute, die sind genetisch nicht zum programmieren geschaffen.
in der regel haben diese leute die regel...
delfiphan
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2684
Erhaltene Danke: 32



BeitragVerfasst: Di 22.03.05 02:47 
Gut, dann eben so :D
Da du angedeutet hast, dass ein String mehrfach vorkommen kann, hab ich das jetzt auch noch korrigiert.
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
Var
 Index : Integer;
begin
 with TStringList.Create do
 try
  LoadFromFile(Filename);
  repeat
   Index := IndexOf('abc');
   if Index >= 0 then
    Delete(Index);
  until Index < 0;
  SaveToFile(Filename);
 finally
  Free;
 end;
end;
mael
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 198
Erhaltene Danke: 39


Delphi XE3 Prof.
BeitragVerfasst: Di 22.03.05 16:09 
user profile iconretnyg hat folgendes geschrieben:
die anzahl der zeilen schon, aber nicht die performance...

edit: siehe mh-nexus.de/optimizingger.htm

Ist in diesem Fall aber nicht schlimm, da es nicht in einer Schleife steht.
Ich denke Optimierung sollte nur dann angewendet werden, wenn es spürbare Vorteile bringt, sonst ist Wartbarkeit wichtiger.

P.S.: Meine Optimierungsseite ist nicht fertig und auch nicht wirklich aktuell, es stimmt zwar was draufsteht, aber es fehlt doch eine Menge.