Autor Beitrag
CrOc
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 48

Win XP
D5 Standard
BeitragVerfasst: Mi 26.11.03 21:00 
Hi,
wie kann ich einzelne Strings als eine txt - Datei speichern, wobei nach jeden String in der txt - Datei eine neue Zeile angefangen werden soll?
obbschtkuche
Gast
Erhaltene Danke: 1



BeitragVerfasst: Mi 26.11.03 21:03 
In der OH (Onlinehilfe) unter Suche in: Delphi-Forum, Delphi-Library TSTRINGLIST
barfuesser
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 324



BeitragVerfasst: Do 27.11.03 10:50 
oder
    AssignFile
    Reset
    Rewrite
    WriteLn
    CloseFile

barfuesser
Udontknow
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2596

Win7
D2006 WIN32, .NET (C#)
BeitragVerfasst: Do 27.11.03 11:05 
Was hat das eigentlich mit Multimedia zu tun? :?

Cu,
Udontknow
CrOc Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 48

Win XP
D5 Standard
BeitragVerfasst: Do 27.11.03 19:02 
Da ich noch ein totaler Noob in Delphi bin, kann ich mit denen Befehlen nicht so viel anfangen!
Ein Beispiel bzw. ein Quellcode, der z.B. einen String in eine Datei namens "test.txt" speichert, wäre nicht schlecht
barfuesser
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 324



BeitragVerfasst: Do 27.11.03 19:13 
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
procedure writeFile(FileName: String);
var
  StrList: TStringList;
begin
  StrList := TStringList.Create;
  try
    StrList.Add('String1');
    StrList.Add('String2');
    StrList.Add('String3');
    StrList.Add('String4');
    StrList.SaveToFile(FileName);
  finally
    FreeAndNil(StrList);
  end;
end;

procedure loadFile(FileName: String);
var
  StrList: TStringList;
  i:       Integer;
begin
  if not FileExists(FileName) then Exit;
  StrList := TStringList.Create;
  try
    StrList.LoadFromFile(FileName);
    if StrList.Count > 0 then
      for i:=0 to StrList.Count - 1 do
        ShowMessage(StrList[i]);
  finally
    FreeAndNil(StrList);
  end;
end;

barfuesser
CrOc Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 48

Win XP
D5 Standard
BeitragVerfasst: Do 27.11.03 19:17 
Thanks,
aber warum muss ich die einzelnen strlist.add's mit try ... finally umgeben?
barfuesser
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 324



BeitragVerfasst: Do 27.11.03 19:21 
Der finally-Block wird auf jedenfall ausgeführt. Dadurch stellst Du sicher, daß die StringList auch definitiv wieder freigegeben wird und nicht bei einer Exception zu einem MemoryLeak führt.

barfuesser
CrOc Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 48

Win XP
D5 Standard
BeitragVerfasst: Do 27.11.03 19:24 
... und freeandnil heißt dann so viel, dass die gesamte Liste gelöscht wird oder?
barfuesser
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 324



BeitragVerfasst: Do 27.11.03 19:27 
FreeAndNil(Object)entspricht in etwa:
ausblenden Delphi-Quelltext
1:
2:
Object.Free
Object := nil

barfuesser
CrOc Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 48

Win XP
D5 Standard
BeitragVerfasst: Do 27.11.03 19:28 
also war doch dann meine vermutung in etwa richtig?
Udontknow
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2596

Win7
D2006 WIN32, .NET (C#)
BeitragVerfasst: Fr 28.11.03 10:13 
Die Liste wird bereits mit Free gelöscht. Da aber die Objektvariable noch immer dann einen Wert hat (im Endeffekt ist sie nur ein Zeiger auf den Speicherbereich des Objekts, in dem Fall der Liste), kann man nicht erkennen, ob es sich um ein noch existierendes Objekt handelt. Daher weisst man nach Free meist NIL zu, sodaß ma doch prüfen kann, ob das Objekt noch existiert (if MyObject<>NIL then ...)
Aus Komfortgründen benutzt man dann FreeAndNIL.

Cu,
Udontknow