Autor Beitrag
Hendi48
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 271



BeitragVerfasst: Mi 27.06.07 20:52 
Hi,
also ich möchte ein programm machen das prüfen kann ob z.B. bereich '1' in einer Ini existiert (und wenn ja in bereich '2' schreiben soll aber das is ja nich schwer)
Ist das überhaupt möglich und wenn nein kann ich das irgendwie anders machen?
Shorty
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 107

Win XP, Ubuntu 8.04
Delphi 7 Prof., Delphi 2005 Pers.
BeitragVerfasst: Mi 27.06.07 20:59 
Delphi-Hilfe hat folgendes geschrieben:
SectionExists (Methode von TCustomIniFile)

Die Methode gibt an, ob die INI-Datei einen Abschnitt enthält.

Delphi-Syntax:

ausblenden Delphi-Quelltext
1:
function SectionExists (const Section: String): Boolean;					


C++ Syntax:

ausblenden Quelltext
1:
bool __fastcall SectionExists(const AnsiString Section);					


Beschreibung

Mit SectionExists kann festgelegt werden, ob die in FileName angegebene INI-Datei einen Abschnitt enthält.

Der String Section bezeichnet den INI-Dateiabschnitt, dessen Vorhandensein von SectionExists festgestellt wird.

SectionExists gibt einen booleschen Wert zurück, der angibt, ob der fragliche Abschnitt enthalten ist.


Also einfach mit if überprüfen und bei else das mit Sektion 2 schreiben.

MfG Shorty
Hendi48 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 271



BeitragVerfasst: Mi 27.06.07 21:18 
ok, danke.
Ich hab das jetz so:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
var
ini: TIniFile;
inipfad: string;
begin
 IniPfad:=ExtractFilePath(Paramstr(0))+'config.ini';
 try
 if ini.sectionexists('1') then
 begin
    Ini:=TIniFile.Create(IniPfad);
    Ini.WriteString('2','Name',form5.name.text);
    Ini.WriteString('2','File','bin\'+form5.name.text+'.txt');
 end
 else
    Ini:=TIniFile.Create(IniPfad);
    Ini.WriteString('1','Name',form5.name.text);
    Ini.WriteString('1','File','bin\'+form5.name.text+'.txt');
 finally
   ini.free;
 end;
end;

aber ich bekomm immer ne access violation wenn ich den button drück. Is da irgendwas verdreht?

EDIT: OOPS mir fällt grad erst auf das der bei sectionexists noch gar nich weiss welche ini O_o das änder ich eben
EDIT2: Ok jetz gehts. Danke für die Hilfe
JayEff
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2971

Windows Vista Ultimate
D7 Enterprise
BeitragVerfasst: Mi 27.06.07 22:58 
user profile iconHendi48 hat folgendes geschrieben:
EDIT: OOPS mir fällt grad erst auf das der bei sectionexists noch gar nich weiss welche ini
Wollte nur der Vollständigkeit halber anmerken: Bei ini := TIniFile.Create(xy) gibst du nicht nur an, welche Datei er eigentlich laden soll, sondern hier reservierst du überhaupt erst Speicher für das TIniFile-Objekt. Und weil du mit ini.SectionExists auf Speicher zugreifen willst, den du nicht reserviert hast, bekommst du eine Zugriffsverletzung (Acces Violation) ;)
Aber das nur der Vollständigkeit halber.
PS: Christian, wie wär's mit "Vollständigkeit-Tags" ? :mrgreen:

_________________
>+++[>+++[>++++++++<-]<-]<++++[>++++[>>>+++++++<<<-]<-]<<++
[>++[>++[>>++++<<-]<-]<-]>>>>>++++++++++++++++++.+++++++.>++.-.<<.>>--.<+++++..<+.
Chatfix
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 1583
Erhaltene Danke: 10

Win 10, Win 8, Win 7, Win Vista, Win XP
VB.net (VS 2015), MsSQL (T-SQL), HTML, CSS, PHP, MySQL
BeitragVerfasst: Do 28.06.07 07:07 
Vielleicht hilft dir auch dieses INI-Tutorial weiter: www.kuknet.de/tutorial1.pdf

_________________
Gehirn: ein Organ, mit dem wir denken, daß wir denken. - Ambrose Bierce
Hendi48 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 271



BeitragVerfasst: Do 28.06.07 10:50 
lol, ich hab irgendwie nen problem..
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:
procedure savetoini;
var
ini: TIniFile;
inipfad: string;
begin
 IniPfad:=ExtractFilePath(Paramstr(0))+'config.ini';
 try
 Ini:=TIniFile.Create(IniPfad);

 if ini.sectionexists('1') then
 begin
    Ini.WriteString('2','Name',form5.name.text);
    Ini.WriteString('2','File','bin\'+form5.name.text+'.txt');
 end;

 if ini.sectionexists('2')=false then
  begin
    Ini.WriteString('1','Name',form5.name.text);
    Ini.WriteString('1','File','bin\'+form5.name.text+'.txt');
  end;

 if ini.sectionexists('2') then
  begin
    Ini.WriteString('3','Name',form5.name.text);
    Ini.WriteString('3','File','bin\'+form5.name.text+'.txt');
  end;

 if ini.sectionexists('3') then
  begin
    Ini.WriteString('4','Name',form5.name.text);
    Ini.WriteString('4','File','bin\'+form5.name.text+'.txt');
  end;

 if ini.sectionexists('4') then
  begin
    Ini.WriteString('5','Name',form5.name.text);
    Ini.WriteString('5','File','bin\'+form5.name.text+'.txt');
  end;

 if ini.sectionexists('5') then
  begin
    Ini.WriteString('6','Name',form5.name.text);
    Ini.WriteString('6','File','bin\'+form5.name.text+'.txt');
  end;

 if ini.sectionexists('6') then
  begin
    Ini.WriteString('7','Name',form5.name.text);
    Ini.WriteString('7','File','bin\'+form5.name.text+'.txt');
  end;

 if ini.sectionexists('7') then
  begin
    Ini.WriteString('8','Name',form5.name.text);
    Ini.WriteString('8','File','bin\'+form5.name.text+'.txt');
  end;

 if ini.sectionexists('8') then
  begin
    Ini.WriteString('9','Name',form5.name.text);
    Ini.WriteString('9','File','bin\'+form5.name.text+'.txt');
  end;

 if ini.sectionexists('9') then
  begin
    Ini.WriteString('10','Name',form5.name.text);
    Ini.WriteString('10','File','bin\'+form5.name.text+'.txt');
  end;

 if ini.sectionexists('10') then
  begin
    Ini.WriteString('11','Name',form5.name.text);
    Ini.WriteString('11','File','bin\'+form5.name.text+'.txt');
  end;

 if ini.sectionexists('11') then
  begin
    Ini.WriteString('12','Name',form5.name.text);
    Ini.WriteString('12','File','bin\'+form5.name.text+'.txt');
  end;

 if ini.sectionexists('12') then
  begin
    Ini.WriteString('13','Name',form5.name.text);
    Ini.WriteString('13','File','bin\'+form5.name.text+'.txt');
  end;

 if ini.sectionexists('13') then
  begin
    Ini.WriteString('14','Name',form5.name.text);
    Ini.WriteString('14','File','bin\'+form5.name.text+'.txt');
  end;

 if ini.sectionexists('14') then
  begin
    Ini.WriteString('15','Name',form5.name.text);
    Ini.WriteString('15','File','bin\'+form5.name.text+'.txt');
  end;

 if ini.sectionexists('15') then
  begin
    Ini.WriteString('16','Name',form5.name.text);
    Ini.WriteString('16','File','bin\'+form5.name.text+'.txt');
  end;

 finally
   ini.free;
 end;
end;

so schreibt der gleich alle 16 slots voll weil wenn 2 ja voll is wird 3 erstellt und dann wieder geprüft ob 3 existiert und dann 14x das gleiche in alle slots geschrieben..nur ich weiß nich was ich ändern soll
Chatfix
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 1583
Erhaltene Danke: 10

Win 10, Win 8, Win 7, Win Vista, Win XP
VB.net (VS 2015), MsSQL (T-SQL), HTML, CSS, PHP, MySQL
BeitragVerfasst: Do 28.06.07 11:03 
enweder du machst eine Schleife und unterbrichst die nach dem Schreiben, oder du setzt einen Flag beim schreiben und fragst den jedesmal im if mit ab.

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
var
  Fertig: Boolean;

if ini.sectionexists('1') then  
 begin  
    Ini.WriteString('2','Name',form5.name.text);  
    Ini.WriteString('2','File','bin\'+form5.name.text+'.txt'); 
    Fertig := True; 
 end;  
 
 if ini.sectionexists('2') and not Fertig then  
  begin  
    Ini.WriteString('3','Name',form5.name.text);  
    Ini.WriteString('3','File','bin\'+form5.name.text+'.txt'); 
   Fertig := True; 
  end;  

//usw...


Allerdings würde er dir trotzdem immer Werte überschreiben.

Wenn du 15 Einträge hast, existiert ja trotzdem die erste Section.

Am besten du speicherst in die INI einen Zähler, den du aufrufst und aktualisierst. so weist du immer wieviel einträge drin sind und wo du weitermachen musst

_________________
Gehirn: ein Organ, mit dem wir denken, daß wir denken. - Ambrose Bierce
Hendi48 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 271



BeitragVerfasst: Do 28.06.07 11:38 
ok ich hab das jetz so mal sehn ob das bei den andern auch geht..
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
 begin
   zähler:=ini.ReadInteger('Zähler','Integer',0);
   form5.label5.caption:=inttostr(zähler);
 end;

 if ini.sectionexists('1')=false then
 begin
    if form5.label5.caption='0' then
    begin
    Ini.WriteString('1','Name',form5.name.text);
    Ini.WriteString('1','File','bin\'+form5.name.text+'.txt');
    end;
 end;

Edit: Jup, klappt wunderbar, nochmals danke!