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



BeitragVerfasst: Mo 30.09.02 12:08 
Hi,
wie kann man bei einer Textdatei mit Endung txt den Inhalt löschen ohne dass man hierfür eine neue Datei erstellt (also nur mit einer Datei arbeitet)?

:shock:
LCS
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1305
Erhaltene Danke: 1

WIN 7, WIN 8
Delphi XE5, Delphi XE, Delphi 2007
BeitragVerfasst: Mo 30.09.02 12:30 
Hi
so ganz verstehe ich dein Problem jetzt nicht. Willst du die Datei komplett löschen dann wäre DeleteFile das einfachste.
Oder soll die leere Datei tatsächlich erhalten bleiben?

Gruss Lothar

_________________
Der BH ist für die Brust, der Plan ist für'n Ar...
gogi2207 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 45



BeitragVerfasst: Mo 30.09.02 12:33 
Leere Datei soll erhalten bleiben damit man danach wieder Text einfügen kann.
LCS
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1305
Erhaltene Danke: 1

WIN 7, WIN 8
Delphi XE5, Delphi XE, Delphi 2007
BeitragVerfasst: Mo 30.09.02 12:36 
Hi
dann könntest du das beispielsweise so machen:
ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
procedure DateiLeeren(Dateiname: string);
var
 lst : TStringList;
begin
  lst : TStringList.Create;
  try
    lst.SaveToFile(Dateiname);
  finally
    lst.free;
  end;
end;

Ist vielleicht net sonderlich elegant, aber sollte funktionieren.

Gruss Lothar

_________________
Der BH ist für die Brust, der Plan ist für'n Ar...
DeCodeGuru
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 1333
Erhaltene Danke: 1

Arch Linux
Eclipse
BeitragVerfasst: Mo 30.09.02 12:49 
Hi LCS,

wie wäre es denn mit:

ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
procedure DateiLeeren(Dateiname: string); 
var 
lst : TStringList; 
begin 
  lst : TStringList.Create; 
  try 
    lst.Clear;
    lst.SaveToFile(Dateiname); 
  finally 
    lst.free; 
  end; 
end;


Das Clear haste wohl vergessen :mrgreen:

_________________
Viele Grüße
Jakob
SvenAbeln
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 334
Erhaltene Danke: 3



BeitragVerfasst: Mo 30.09.02 12:49 
Hallo,

erstell die Datei doch einfach mit rewrite neu

Aus der Hilfe
Zitat:

procedure Rewrite(var F: File [; Recsize: Word ] );
[...]
Rewrite erstellt eine neue externe Datei unter dem F zugewiesenen Namen.
Ist schon eine gleichnamige externe Datei vorhanden, wird sie gelöscht und an ihrer Stelle die neue Datei angelegt.
gogi2207 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 45



BeitragVerfasst: Mo 30.09.02 12:52 
Habe bis jetzt folgenden Quelltext:
ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
Procedure tform1.Ausgeben(Inputfile:string); 
var counter : integer; 
    Textzeile : string; 
    F1 : textfile; 
begin 
  AssignFile(F1, Inputfile); 
  reset(F1); 
  append(F1); 
  For counter := 0 to stringgrid1.rowcount do 
  Begin 
    Textzeile := stringgrid1.rows[counter].GetText; 
    write(F1,Textzeile); 
  end; 
  closefile(F1); 
end;

Er liest also aus einem Stringgrid aus und hängt es hinten an eine Textdatei an. Er soll den vorhandenen Text dieser Datei jedoch überschreiben. Und dafür sollte er zunächst den Inhalt der Textdatei löschen.
LCS
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1305
Erhaltene Danke: 1

WIN 7, WIN 8
Delphi XE5, Delphi XE, Delphi 2007
BeitragVerfasst: Mo 30.09.02 12:59 
Hi
wenn der Inhalt der Textdatei doch sowieso immer neu geschrieben wird, dann
ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
Procedure tform1.Ausgeben(Inputfile:string); 
var counter : integer; 
Textzeile : string; 
F1 : textfile; 
begin 
AssignFile(F1, Inputfile); 
rewrite(F1); // <== Änderung ==>

For counter := 0 to stringgrid1.rowcount do 
Begin 
Textzeile := stringgrid1.rows[counter].GetText; 
write(F1,Textzeile); 
end; 
closefile(F1); 
end;


@CodeGuru
DeCodeGuru hat folgendes geschrieben:

Das Clear haste wohl vergessen

Ne, aber weil ich schreibfaul bin, geh ich mal davon aus, dass eine Stringlist nachm Erzeugen leer is :mrgreen:

Gruss Lothar

_________________
Der BH ist für die Brust, der Plan ist für'n Ar...


Zuletzt bearbeitet von LCS am Mo 30.09.02 13:10, insgesamt 1-mal bearbeitet
SvenAbeln
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 334
Erhaltene Danke: 3



BeitragVerfasst: Mo 30.09.02 13:01 
Hallo,

Lösche mal diese beiden Zeilen
Zitat:

reset(F1);
append(F1);

( das reset macht hier sowieso nichts da append die Datei wieder
schließt um sie dann zum anhängen neuer daten wieder zu öffnen )

und ersetze sie durch

ausblenden Quelltext
1:
rewrite(F1);					


:lol:
gogi2207 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 45



BeitragVerfasst: Mo 30.09.02 13:43 
Hatte es schon mit rewrite versucht aber mein Problem ist, das mehrer Personen auf die Datei zugreifen und wenn durch rewrite eine neue Datei erstellt wird führt dies doch zu Komplikationen oder nicht?