Autor Beitrag
Luckie
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Mo 19.07.04 15:17 
So, habe mal eben was zusammengeschrieben:
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:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
type  
  TPerson = record  
    Name: String[255];  
    Vorname: String[255];  
  end;

  TPersonArray = array of TPerson;


var
  Form1: TForm1;
 
implementation  

{$R *.dfm}

procedure AppendRecord(Filename: String; MyRecord: TPerson);
var
  fs: TFileStream;
begin
  fs := TFileStream.Create(Filename, fmOpenReadWrite);
  try
    fs.Seek(fs.Size, soFromBeginning);
    fs.WriteBuffer(MyRecord, sizeof(TPerson));
  finally
    FreeAndNil(fs);
  end;
end;

procedure CreateNewFile(Filename: String; MyRecord: TPerson);
var
  fs: TfileStream;
begin
  fs := TFileStream.Create(Filename, fmCreate);
  try
    fs.WriteBuffer(MyRecord, sizeof(TPerson));
  finally
    FreeAndNil(fs);
  end;
end;

function GetRecordCount(Filename: String): Cardinal;
var
  fs: TFileStream;
begin
  fs := TFileStream.Create(Filename, fmOpenRead);
  try
    result := fs.Size div sizeof(TPerson);
  finally
    FreeAndNil(fs);
  end;
end;

function GetRecordByIndex(Filename: String; Index: Cardinal): TPerson;
var
  fs: TFileStream;
  OffSet: Integer;
begin
  fs := TfileStream.Create(Filename, fmOpenRead);
  try
    OffSet := Index * sizeof(TPerson);
    fs.Seek(OffSet, soFromBeginning);
    fs.ReadBuffer(result, sizeof(TPerson));
  finally
    FreeAndNil(fs);
  end;
end;

function GetAllRecords(Filename: String): TPersonArray;
var
  fs: TFileStream;
  RecCount: Cardinal;
  i: Integer;
  OffSet: Integer;
begin
  RecCount := GetRecordCount(Filename);
  setlength(result, RecCount);
  fs := TFileStream.Create(Filename, fmOpenRead);
  try
    for i := 0 to RecCount-1 do
    begin
      fs.Seek(i*sizeof(TPerson), soFromBeginning);
      fs.ReadBuffer(result[i], sizeof(TPerson));
    end;
  finally
    FreeAndNil(fs);
  end;
end;


procedure TForm1.Button1Click(Sender: TObject);
var
  Person: TPerson;
begin
  Person.Name := Edit1.Text;
  Person.Vorname := Edit2.Text;
  if FileExists('g:\foo\Test.dat') then
    AppendRecord('g:\foo\Test.dat', Person)
  else
    CreateNewFile('g:\foo\Test.dat', Person);
  Edit1.Text := '';
  Edit2.Text := '';
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  ShowMessage(IntToStr(GetRecordCount('g:\foo\Test.dat')));
end;

procedure TForm1.Button3Click(Sender: TObject);
var
  Person: TPerson;
begin
  Person := GetRecordByIndex('g:\foo\Test.dat', 0);
  Edit1.Text := Person.Name;
  Edit2.Text := person.Vorname;
end;

procedure TForm1.Button4Click(Sender: TObject);
var
  Personen: TPersonArray;
  Person: TPerson;
  i: Integer;
begin
//  Personen := GetAllRecords('g:\foo\Test.dat');
//  for i := 0 to length(Personen)-1 do
//  begin
//    ListBox1.Items.Add(Personen[i].Name+', '+Personen[i].Vorname);
//  end;

  // oder:
  for i := 0 to GetRecordCount('g:\foo\Test.dat')-1 do
  begin
    Person := GetRecordByIndex('g:\foo\Test.dat', i);
    Listbox1.Items.Add(Person.Name+', '+Person.Vorname);
  end;
end;

Fehlt nur noch was zum Löschen und zum Suchen. Aber etwas sollst du auch selber machen. ;)
Luckie
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Mo 19.07.04 15:22 
Mike_C hat folgendes geschrieben:
Einziger nachteil bei der Stream-Methode sind relativ große Dateien!
mit Blockread/Blockwrite brauche ich bei einem Record mit 12 Einträgen (alles strings) unter 1 KB bei der Stream-Methode ganze 3 KB. kann man das irgendwie relativieren?

Das kann nichts ein. Die Datei wird immer genauso groß wie den Record mal Anzahl der Records.
Mike_C Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 207

Win XP
D7 Enterprise
BeitragVerfasst: Mo 19.07.04 17:03 
@UdontKnow:
Och, ich hab die Dateigröße dadurch immerhin von 3 KB auf 450 Bytes gebracht. ich glaube das kann man ganz gut akzeptieren

_________________
Life is, what some people call a mystery. To me life's just a lesson, you're learning when you're through. So why do we try to understand?


Zuletzt bearbeitet von Mike_C am Mo 19.07.04 17:11, insgesamt 1-mal bearbeitet
Mike_C Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 207

Win XP
D7 Enterprise
BeitragVerfasst: Mo 19.07.04 17:06 
Luckie hat folgendes geschrieben:
Mike_C hat folgendes geschrieben:
Einziger nachteil bei der Stream-Methode sind relativ große Dateien!
mit Blockread/Blockwrite brauche ich bei einem Record mit 12 Einträgen (alles strings) unter 1 KB bei der Stream-Methode ganze 3 KB. kann man das irgendwie relativieren?

Das kann nichts ein. Die Datei wird immer genauso groß wie den Record mal Anzahl der Records.


ich hatte mit string[255] gearbeitet: 12*256 = 3072 = 3KB. Oder etwa nicht?!

_________________
Life is, what some people call a mystery. To me life's just a lesson, you're learning when you're through. So why do we try to understand?
Mike_C Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 207

Win XP
D7 Enterprise
BeitragVerfasst: Mo 19.07.04 17:09 
@Luckie: Thx für deine Mühe! Werd im Sourcecode drauf verweisen, dass Teile von dir sind :D

So den Rest bekomme ich glaub ich hin 8) sollte ja nicht mehr so schwer sein!

Gruß Mike_C

_________________
Life is, what some people call a mystery. To me life's just a lesson, you're learning when you're through. So why do we try to understand?
Luckie
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Mo 19.07.04 17:19 
Mike_C hat folgendes geschrieben:
Luckie hat folgendes geschrieben:
Mike_C hat folgendes geschrieben:
Einziger nachteil bei der Stream-Methode sind relativ große Dateien!
mit Blockread/Blockwrite brauche ich bei einem Record mit 12 Einträgen (alles strings) unter 1 KB bei der Stream-Methode ganze 3 KB. kann man das irgendwie relativieren?

Das kann nichts ein. Die Datei wird immer genauso groß wie den Record mal Anzahl der Records.


ich hatte mit string[255] gearbeitet: 12*256 = 3072 = 3KB. Oder etwa nicht?!

Und mit den Pascal Routinen BlockRead/BlockWrite wird es nicht anders sein. Denn meines Wissen komprimieren sie nicht von Haus aus. ;)
Mike_C Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 207

Win XP
D7 Enterprise
BeitragVerfasst: Mo 19.07.04 17:24 
@Luckie: nein, mit Blockread und Blockwrite schreibe ich nur soviele bytes, wie ich wirklich brauche! Deshalb ja die scherze mit Blockwrite(F, S, l*sizeof(char));

Gruß
Mike_C

_________________
Life is, what some people call a mystery. To me life's just a lesson, you're learning when you're through. So why do we try to understand?
Luckie
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Mo 19.07.04 17:32 
Dann aber viel Spass beim Zugreifen, wenn du nicht weißt, wo was anfängt und wo was aufhört.
Mike_C Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 207

Win XP
D7 Enterprise
BeitragVerfasst: Mo 19.07.04 17:44 
Also Danke noch mal und man hört von sich bei Problemen 8)

_________________
Life is, what some people call a mystery. To me life's just a lesson, you're learning when you're through. So why do we try to understand?