Autor Beitrag
Blamaster
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 164



BeitragVerfasst: Di 21.08.07 18:48 
Hi,

ich möchte gerne aus einer TStringList die zahl aus der 1. Zeile auslesen und dann um 1 erhöhen. Allerdings bekomme ich das nicht so wirklich hin.

Kann mir da wer helfen ?

Mit

inc (sl2[0]); funktioniert es leider nicht.
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10185
Erhaltene Danke: 1261

W11x64
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Di 21.08.07 18:54 
Moin und :welcome: im Forum!

user profile iconBlamaster hat folgendes geschrieben:
ich möchte gerne aus einer TStringList die zahl aus der 1. Zeile auslesen und dann um 1 erhöhen.
[...]
Mit Inc(sl2[0]); funktioniert es leider nicht.

Das ist kein Wunder, denn die "Zahl" ist ja auch eine Zeichenkette - da kann man nix addieren! :mahn: ;)

Du musst den String erstmal in eine Zahl umwandeln:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
var
  IntVar: Integer;
begin
  IntVar := IntToStr(sl2[0]);

Dann kannst du 1 dazu zählen:
ausblenden Delphi-Quelltext
1:
2:
3:
IntVar := IntVar +1;
// Alternative:
Inc(IntVar);

Und dann mußt du aus dem Integer wieder einen String machen:
ausblenden Delphi-Quelltext
1:
ShowMessage(IntToStr(IntVar));					

cu
Narses

//EDIT: Index-Blödsinn entfernt, s.u. :oops:

_________________
There are 10 types of people - those who understand binary and those who don´t.


Zuletzt bearbeitet von Narses am Di 21.08.07 20:41, insgesamt 1-mal bearbeitet
Blamaster Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 164



BeitragVerfasst: Di 21.08.07 19:42 
Danke soweit schonmal.

Ich möchte umsetzten das wenn die datei datei.txt nicht existiert diese zuerst erstellt wird und der Wert 0 eingetragen.

Wenn die Datei allerdings schon existiert soll er einfach wie normal weiter machen.

Mein momentaner code sieht so aus nur der will irgendwie noch nicht so ganz.

ausblenden 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:
if not FileExists('C:\datei.txt') then
sl:=TStringList.Create;
try
sl.Add(IntToStr(Neu));
sl.SaveToFile('C:\datei.txt');
finally
sl.free;
end;

sl2:=TStringList.Create;
try
sl2.LoadFromFile('C:\datei.txt');
Id := StrToInt(sl2[0]);
inc (Id);
ShowMessage(IntToStr(Id));
finally
sl2.free
end;

sl:=TStringList.Create;
try
sl.Add(IntToStr(Id));
sl.SaveToFile('C:\datei.txt');
finally
sl.free;
end;
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10185
Erhaltene Danke: 1261

W11x64
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Di 21.08.07 19:43 
Moin!

Bitte für neue Fragen einen neuen Thread aufmachen - das gibt sonst nur Chaos! :mahn: ;)

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
jakobwenzel
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1889
Erhaltene Danke: 1

XP home, ubuntu
BDS 2006 Prof
BeitragVerfasst: Di 21.08.07 20:30 
user profile iconNarses hat folgendes geschrieben:
(nebenbei: Stringlisten fangen mit Index 1 an)

Nicht wirklich.

Stringlisten gehen von 0 bis Count -1 und Strings von 1 bis length.

_________________
I thought what I'd do was, I'd pretend I was one of those deaf-mutes.
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10185
Erhaltene Danke: 1261

W11x64
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Di 21.08.07 20:34 
Moin!

user profile iconjakobwenzel hat folgendes geschrieben:
user profile iconNarses hat folgendes geschrieben:
(nebenbei: Stringlisten fangen mit Index 1 an)

Nicht wirklich.

Ouw... :autsch: :oops:

user profile iconjakobwenzel hat folgendes geschrieben:
Stringlisten gehen von 0 bis Count -1 und Strings von 1 bis length.

Klar, ich war im falschen Film :nut: Danke für die Korrektur. :?

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.