Autor Beitrag
beljo
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 129



BeitragVerfasst: Mo 24.07.06 09:46 
Hallo Leute,
ich würde gern ein bestehendes Textfile manipulieren.
In meinen Fall eine einzige Zeile ändern:
hier schon mal mein Ansatz:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
var   
  F: TextFile;  
S,K: string;  
begin
  s:=Meine_aenderung;
    
  if OpenDialog1.Execute then            { Öffnen-Dialogfeld anzeigen }  
  begin  
    AssignFile(F, OpenDialog1.FileName); { Datei in Dialogfeld ausgewählt }  
    Reset(F);  
     Repeat
     //.. Leider weis ich nicht wie ich eine Zeile Weiter gehe
     

     Unitl (EoF(F)) or (???) ;  {Abruch wenn die zu bearbeitende Zeile gefunden wurde, weis aber nicht wie es gehen soll???}
    
    CloseFile(F);  
  end;  
end;

So in etwa will ich es realisieren, weis aber nicht wie ich eine Zeile weiter runter kommen kann und wo und wie ich die Zeile zwischenspeichere, hoffe jemand kann mir helfen.
beljo Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 129



BeitragVerfasst: Mo 24.07.06 10:55 
hallo ich hab schon etwas rumprobiert
hier:
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:
procedure TForm1.FormCreate(Sender: TObject);

var
  sList: TStringList;
  i: Integer;
  MyString : string;
begin
  MyString :='$';
 if OpenDialog1.execute then
  begin

   memo1.Lines.LoadFromFile(Opendialog1.filename);

   for i := 0 to memo1.Lines.Count-1 do
    begin
     if MyString =  Memo1.Text[i] then Memo1.Lines[i]:=('$ COMI C:\fem\work\a.comi MODE=CONT') else ShowMessage('FALSCH');
      // Auf dem Memofeld ist leider nicht zu sehen ob es eine änderung gibt
     memo1.Lines.saveToFile(Opendialog1.filename);  // WARUM wird an dieser Stelle nicht der geänderte Wert übergeben?
     sList.Free;
    end;
  end;
end;

Leider funzt es nicht. In Zeile 16 soll ja wenn der String gefunden ist die Zeile auch überschrieben werden.
Wie kann ich denn einen einzelnen String am beginn der Zeile überprüfen?
das DollarZeichen ist an der ersten Stelle der Zeile die Überschrieben werden soll
azubi_20
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 593

WinXP SP2, Ubuntu 8.4
D7 Enterp., D2005 Prof., Java (Eclipse 3.4.0)
BeitragVerfasst: Mo 24.07.06 10:57 
Das kann man ganz gut mit Stringlisten machen. Hier mal ein Beispiel :

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:
var
S,K: string;
SL : TStringlist;
i : Integer;
begin
  try
    SL:=TStringlist.Create;  {Stringliste bereit machen}
    SL.Clear;

      s:='Meine_aenderung';

      if OpenDialog1.Execute then            { Öffnen-Dialogfeld anzeigen }
      begin
        sl.LoadFromFile(OpenDialog1.FileName);
        K:=sl.Strings[3];                    {4. Zeile wird in K gespeichert}
        sl.Strings[3]:= s;                   {4. Zeile wird verändert}
        sl.SaveToFile(OpenDialog1.FileName); {Datei wieder abspeichern}
      end;

  finally
    sl.Free;                             {Stringliste frei machen}
  end;

end;


EDIT : zu spät
azubi_20
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 593

WinXP SP2, Ubuntu 8.4
D7 Enterp., D2005 Prof., Java (Eclipse 3.4.0)
BeitragVerfasst: Mo 24.07.06 11:14 
also, nochmal modifiziert. Es werden alle Zeilen durchgegangen und das 1.Zeichen überprüft. Falls du noch ne Ausgabe im Memo wünscht, musst du dir das noch machen.

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:
27:
28:
29:
30:
var
MyString: string;
SL : TStringlist;
i : Integer;
begin
MyString := '$';

  try
    SL:=TStringlist.Create;  {Stringliste bereit machen}
    SL.Clear;

      if OpenDialog1.Execute then            { Öffnen-Dialogfeld anzeigen }
      begin
        sl.LoadFromFile(OpenDialog1.FileName);

        For i := 0 to sl.count -1 do
          begin
           if sl.Strings[i][1] = MyString then  {sucht nach dem 1. Zeichen}
              sl.Strings[i] := '$ COMI C:\fem\work\a.comi MODE=CONT';
          end;


        sl.SaveToFile(OpenDialog1.FileName); {Datei wieder abspeichern}
      end;

  finally
    sl.Free;                             {Stringliste frei machen}
  end;

end;
beljo Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 129



BeitragVerfasst: Mo 24.07.06 11:53 
@ Azubi_20 Hab Deinen Code 1 zu 1 übernommen und bekomme eine Fehlermeldung wenn ich ein File einlesen will.
Fehlermeldung lautet:
Zugriffsverletztung bei Adresse ...
Kannst Du Die den Source-Code nochmal anschauen finde den Fehler nicht.
beljo Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 129



BeitragVerfasst: Mo 24.07.06 12:17 
@ Azubi_20
Es liegt wohl daran das ich im File auch Leere Lines habe.
so sieht mein Textfile aus:
abc
def

$ COMI C:\...


Leider weis ich nicht wie ich das abfangen soll?
hat jemand eine Idee?
Der Quellcode hat sich nicht verändert.
azubi_20
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 593

WinXP SP2, Ubuntu 8.4
D7 Enterp., D2005 Prof., Java (Eclipse 3.4.0)
BeitragVerfasst: Mo 24.07.06 12:42 
user profile iconbeljo hat folgendes geschrieben:
@ Azubi_20
Es liegt wohl daran das ich im File auch Leere Lines habe.


Ja, das ist richtig. So kann man das abfangen :

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:
27:
28:
29:
30:
var
MyString: string;
SL : TStringlist;
i : Integer;
begin
MyString := '$';

  try
    SL:=TStringlist.Create;  {Stringliste bereit machen}
    SL.Clear;

      if OpenDialog1.Execute then            { Öffnen-Dialogfeld anzeigen }
      begin
        sl.LoadFromFile(OpenDialog1.FileName);

        For i := 0 to sl.count -1 do
          begin
           if (length(sl.Strings[i])>0) AND // überprüfen, ob Zeile gefüllt ist
              (sl.Strings[i][1] = MyString) then  {sucht nach dem 1. Zeichen}
              sl.Strings[i] := '$ COMI C:\fem\work\a.comi MODE=CONT';
          end;


        sl.SaveToFile(OpenDialog1.FileName); {Datei wieder abspeichern}
      end;

  finally
    sl.Free;                             {Stringliste frei machen}
  end;
end;
beljo Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 129



BeitragVerfasst: Mo 24.07.06 13:15 
@ Azubi_20 oh ja so kann man es auch machen :)
Hatte es auch schon hinbekommen, wohl nicht so eleganz aber auch ganz ok
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
        For i := 0 to sl.count -1 do
          begin
           if sl.Strings[i] = '' then
              sl.Strings[i]   :=''
         else   if (sl.Strings[i][1] = MyString)//..
:wink:
Danke nochmals
azubi_20
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 593

WinXP SP2, Ubuntu 8.4
D7 Enterp., D2005 Prof., Java (Eclipse 3.4.0)
BeitragVerfasst: Mo 24.07.06 13:58 
Bitte schön... :)