Autor Beitrag
CodeRed
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 45

Win 2000
D7 Pers
BeitragVerfasst: Sa 12.03.05 17:27 
Hi!
Ich hab ein dynamisches Array (Strings), das is in eine Datei speichern und später auch wieder laden möchte.
Hab schon gesucht, aber noch keine Lösung gefunden. Die Datei muss ich selber nicht lesen können.
Wär echt super, wenn ihr mir weiterhelfen könntet!!
Thx
wulfskin
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1349
Erhaltene Danke: 1

Win XP
D5 Pers (SSL), D2005 Pro, C, C#
BeitragVerfasst: Sa 12.03.05 17:42 
Hallo,

so gehts:
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:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
type
  TDynStrArray = Array of String;

procedure WriteString(const AStr: String; const AStream: TStream);
var
  Len: Word;
begin
  Len := Length(AStr);
  AStream.Write(Len, SizeOf(Len));
  if Len > 0 then
    AStream.Write(Pointer(AStr)^, Len);
end;

function ReadString(const AStream: TStream): String;
var
  Len: Word;
begin
  AStream.Read(Len, SizeOf(Len));
  SetLength(Result, Len);
  if Len > 0 then begin
    if AStream.Read(Pointer(Result)^, Len) <> Len then
      Result := '';
  end;
end;

procedure SaveToFile(const AStrArray: TDynStrArray; const AFileName: String);
var
  I: Integer;
  FileStream: TFileStream;
begin
  FileStream := TFileStream.Create(AFileName, fmCreate or fmShareExclusive);
  with FileStream do begin
    try
      I := Length(AStrArray);
      Write(I, SizeOf(I));
      for I := Low(AStrArray) to High(AStrArray) do
        WriteString(AStrArray[I], FileStream);
    finally
      Free;
    end;
  end;
end;

procedure LoadFromFile(var AStrArray: TDynStrArray; const AFileName: String);
var
  I: Integer;
  FileStream: TFileStream;
begin
  FileStream := TFileStream.Create(AFileName, fmOpenRead or fmShareDenyWrite);
  with FileStream do begin
    try
      Read(I, SizeOf(I));
      SetLength(AStrArray, I);
      for I := Low(AStrArray) to High(AStrArray) do
        AStrArray[I] := ReadString(FileStream);
    finally
      Free;
    end;
  end;
end;
Gruß Hape!

_________________
Manche antworten um ihren Beitragszähler zu erhöhen, andere um zu Helfen.
delfiphan
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2684
Erhaltene Danke: 32



BeitragVerfasst: Sa 12.03.05 18:09 
:idea: Anstatt ein dynamisches Array mit Strings könntest du eine TStringList verwenden. Da sind viele Funktionen wie das Speichern in eine Datei schon drin.
wulfskin
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1349
Erhaltene Danke: 1

Win XP
D5 Pers (SSL), D2005 Pro, C, C#
BeitragVerfasst: Sa 12.03.05 19:04 
user profile icondelfiphan hat folgendes geschrieben:
:idea: Anstatt ein dynamisches Array mit Strings könntest du eine TStringList verwenden. Da sind viele Funktionen wie das Speichern in eine Datei schon drin.
Stimmt eigentlich, aber manchmal sieht man den Wald vor lauter Bäumen nicht! ;)

_________________
Manche antworten um ihren Beitragszähler zu erhöhen, andere um zu Helfen.
CodeRed Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 45

Win 2000
D7 Pers
BeitragVerfasst: Sa 12.03.05 19:44 
hi!
wie deklariere in nun mein array??
habs jetzt so gemacht: ArrayName : TDynStrArray (bisher hatte ich es ArrayName: Array Of Array Of String).

so, wie ich es jetzt habe bekomme ich immer die fehlermeldung: "Auf Element 0 kann nicht zugegriffen werden - 'Length' oder 'SetLenght' verwenden"

PS: ich werde die andere Variante gleich versuchen, wär auch trotzdem super, wenn ihr mir hier auch weiterhelfen könntet!!
delfiphan
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2684
Erhaltene Danke: 32



BeitragVerfasst: So 13.03.05 16:50 
Du musst zuerst angeben, wie gross dein dynamisches Array ist. Erst dann wird Speicher für das Array alloziert. Wie es die Fehlermeldung schon sagt musst du die Grösse des Arrays mit SetLength definieren.
ausblenden Delphi-Quelltext
1:
SetLength(ArrayName, 8);					

Danach kannst du auf ArrayName[0] bis ArrayName[7] zugreifen.
CodeRed Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 45

Win 2000
D7 Pers
BeitragVerfasst: So 13.03.05 17:11 
das hab ich schon gemacht. funktioniert auch normalerweise.
nur wenn ich den code oben einbaue nicht mehr.
wie muss ich mein array da angeben?? (vorher hatte ich Array of Array of string)
delfiphan
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2684
Erhaltene Danke: 32



BeitragVerfasst: So 13.03.05 17:22 
Die Funktionen sind nicht dafür gebaut ein Array of Array of String abzuspeichern.