Autor Beitrag
boozzz
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 66

Win XP, Win 2000, SuSe Linux

BeitragVerfasst: Mi 04.01.06 14:33 
Hallo,
wenn ich den Text "Computer gewonnen" in die dritte Zeile einer Textdatei schreiben will, schreibe ich:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
  
var highscore:Tstrings;
begin
  highscore:=tstringlist.Create;
  highscore.Strings[4];
  highscore.Text:='Computer gewonnen';
  highscore.SaveToFile('Testdatei.txt');
  highscore.free;
end;


Beim Ausführen erhalte ich eine Fehlermeldung (irgendwas mit Exception). Woran liegt das?

Wie kann ich herausfinden, wieviele Zeilen von der Textdatei beschrieben sind?

Viele Grüße,
boozzz
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10185
Erhaltene Danke: 1261

W11x64
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Mi 04.01.06 16:01 
Moin!

Du kannst nicht in eine bestimmte Zeile einer Textdatei schreiben, ohne die Daten in den Speicher zu laden (naja, theoretisch schon, aber das führt hier viel zu weit). Mach das mal so:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
var
  highscore: TStringList;  
begin  
  highscore := TStringList.Create;  
  highscore.LoadFromFile('Testdatei.txt');
  if (highscore.Count >= 3) then
    highscore.Strings[3] := 'Computer gewonnen';  
  highscore.SaveToFile('Testdatei.txt');  
  highscore.Free;  
end;

Die Anzahl der Zeilen in der Datei enthält demnach .Count auf die StringListenvariante bezogen.

cu
Narses
Horst_H
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1654
Erhaltene Danke: 244

WIN10,PuppyLinux
FreePascal,Lazarus
BeitragVerfasst: Mi 04.01.06 16:06 
Hallo,

die Stringliste hat nach Create noch keine Daten und damit keine Zeilen und hat als Anzahl auch 0.

highscore.Count ist also 0.
Du kannst eine Datei mit highscore.LoadfromFile('C:\????.???'); laden.
Die Anzahl der Zeilen ist highscore.Count.
Diese sind von 0 bis Count-1 durchnummeriert.
die 3.te Zeile ist also highscore[2].
Mit highscore.Capacity = ??; kannst Du auch im voraus die Anzahl der Strings festlegen und darauf zugreifen( alle ersteinmal '').

Also
ausblenden Delphi-Quelltext
1:
2:
highscore.Capacity := 3;
highscore[2] :='Computer hat gewonnen';


Gruss Horst

Moderiert von user profile iconChristian S.: Delphi-Tags hinzugefügt
Lannes
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2352
Erhaltene Danke: 4

Win XP, 95, 3.11, IE6
D3 Prof, D4 Standard, D2005 PE, TurboDelphi, Lazarus, D2010
BeitragVerfasst: Mi 04.01.06 17:10 
Hallo,
user profile iconHorst_H hat folgendes geschrieben:
...
Mit highscore.Capacity = ??; kannst Du auch im voraus die Anzahl der Strings festlegen und darauf zugreifen( alle ersteinmal '').
Also
ausblenden Delphi-Quelltext
1:
2:
highscore.Capacity := 3;
highscore[2] :='Computer hat gewonnen';
:?
und in Zeile 2 kommt dann die bekannte Fehlermeldung:
"Der Listenindex überschreitet das Maximum" (oder so ähnlich).
Der Wert von Capacity entspricht der Anzahl von Einträgen in der String-Liste, für die Speicher reserviert wurde.
Mit Capacity werden keine Items angelegt.
Beispiel:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
sl.add('');
sl.add('');
sl.add('');
sl.add('');
showmessage(IntToStr(sl.Count));      //= 4
showmessage(IntToStr(sl.Capacity));   //= 4
sl.Delete(3);
showmessage(IntToStr(sl.Count));      //= 3
showmessage(IntToStr(sl.Capacity));   //= 4
sl.Capacity := 40;
showmessage(IntToStr(sl.Count));      //= 3
showmessage(IntToStr(sl.Capacity));   //= 40

_________________
MfG Lannes
(Nichts ist nicht Nichts) and ('' <> nil ) and (Pointer('') = nil ) and (@('') <> nil )
boozzz Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 66

Win XP, Win 2000, SuSe Linux

BeitragVerfasst: Do 05.01.06 15:37 
Danke für eure Antworten,
leider kommt bei mit immer noch die selbe Fehlermeldung (Exception). Ich hab jetzt schon verschiedene Möglichkeiten durchprobiert. Warum funktioniert das folgende Testprogramm nicht?
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
procedure TForm1.Button1Click(Sender: TObject);
var highscore:TStrings;
begin
  highscore:=TStringList.Create;
  highscore.LoadFromFile('Testdatei.txt');

  highscore.Capacity:=10;
  highscore.SaveToFile('Testdatei.txt');

  highscore[2]:='Computer hat gewonnen';
  highscore.SaveToFile('Testdatei.txt');
  highscore.Free;
end;
end.
Ich will eigentlich nur den Text "Computer hat gewonnen" in die dritte Zeile schreiben.

Den Befehl highscore.Capacity hab ich auch noch nicht so ganz verstanden. Kann ich damit die Anzahl der Zeilen in meiner Textdatei festlegen?
wwwdirk
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 38


D6 Prof / D2005 Prof
BeitragVerfasst: Do 05.01.06 15:54 
Titel: Textdatei
Hallo,

falls die Datei vorhanden ist, dürfte dein prog funktionieren, sofern auch mindestens 3 Zeilen vorhanden sind.

Capacity reserviert ausschliesslich Speicherplatz, fügt aber keine neuen Elemente der Stringliste hinzu.

Also folgendes wäre ausführlicher:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
//..
aFile := 'c:\temp\testdatei.txt'; // Pfad nicht vergessen !
with TStringList.Create do
  try
  begin
    if FileExists (aFile) then  
      LoadFromFile (aFile);
  
    if Count < 3 then
      for i := Count+1 to 3 do
        Add ('');
   
    Strings[2] := 'Computer hat gewonnen';

    SaveToFile (aFile); 
  end;
  finally
    Free;
  end;

// ...


Viele Grüße
Dirk
boozzz Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 66

Win XP, Win 2000, SuSe Linux

BeitragVerfasst: Sa 07.01.06 23:44 
Titel: neue Textdatei erstellen
DANKE, so weit funktioniert alles. Wie kann ich nun eine neue Textdatei erstellen, falls keine vorhanden sein sollte (wenn FileExists('testdatei.txt') unwahr ist)?
stifflersmom
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 194

XP /XP PRO/ SuSE div.
D1 - D7, BDS 2006
BeitragVerfasst: So 08.01.06 15:02 
Da muß gar nichts weiter gemacht werden,
denn StringList.SaveTofile wird dann eben
eine neue Datei erstellt, Du mußt dich da
um gar nichts weiter kümmern.

Moin
boozzz Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 66

Win XP, Win 2000, SuSe Linux

BeitragVerfasst: So 08.01.06 20:28 
Die Fehlermeldung beim compilieren kommt leider trotzdem.
Lannes
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2352
Erhaltene Danke: 4

Win XP, 95, 3.11, IE6
D3 Prof, D4 Standard, D2005 PE, TurboDelphi, Lazarus, D2010
BeitragVerfasst: So 08.01.06 22:26 
Hallo,

wie sieht Dein Code den jetzt aus?
Würdest Du Deinen Code den Du aktuell benutzt hier posten könnte man mehr dazu was sagen. :wink:

_________________
MfG Lannes
(Nichts ist nicht Nichts) and ('' <> nil ) and (Pointer('') = nil ) and (@('') <> nil )
boozzz Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 66

Win XP, Win 2000, SuSe Linux

BeitragVerfasst: Mi 11.01.06 19:10 
Sorry, hab in den falschen Thread geschrieben. Problem ist gelöst. :P