当文件不存在时创建该文件,否则打开该文件向文件末尾添加内容。

解决方案 »

  1.   

    我用TFileStream 没有试成功
      

  2.   

    TFileStream是没法支持你的这种需要,但办法总是有的,如下代码所示:
    procedure TForm1.Button1Click(Sender: TObject);
    var fs:TFileStream;
        s:string;
        fn:string;
    begin
        fn:= 'd:\temp\test.txt';
        if not FileExists(fn) then
             FileClose(FileCreate(fn));
        fs:= TFileStream.Create(fn,fmOpenReadWrite);
        try
           fs.Seek(0,soFromEnd);
           s:= DateTimeToStr(Now)+ chr(13)+chr(10);
           fs.Write(PChar(s)^,Length(s));
        finally
           fs.Free;
        end;
    end;
      

  3.   

    unit Log;interfaceuses
      SysUtils, Forms, Dialogs;procedure MyAddStr(MyStr:string);
    implementation
    procedure  MyAddStr(MyStr:string);
    Var
    MothodFile:TextFile;
    FileName:String;
    FileOpened:Boolean;
    i,j:Integer;
    Str1:array[0..100] of string;
    str4:string;
    Begin
      i:=0;
      FileName:=ExtractFilePath(Application.Exename)+'log.txt';
      AssignFile(MothodFile,FileName);
      Try
        Reset(MothodFile);
        FileOpened:=True;
      Except
      On EInOutError Do
      Begin
        Try
        if FileExists(FileName)=False then
           Begin
             ReWrite(MothodFile);
             FileOpened:=True;
           End
        Else
          Begin
            FileOpened:=False;
            ShowMessage('文件不能打开');
          End;
        Except
          On EInOutError Do
            Begin
                FileOpened:=False;
                ShowMessage('文件不能创建');
            End;
        End;
      End;
    End;
    if fileopened=false then exit;
    While not eof(MothodFile) and (i<99) do
      Begin
        Readln(MothodFile,Str4);
        Str1[i]:=trim(str4);
        inc(i);
       end;
    CloseFile(MothodFile);
    AssignFile(MothodFile,FileName);
    ReWrite(MothodFile);
    Writeln(MothodFile,mystr+'  At:'+datetimetostr(now));
    for j:=0 to i do
      Begin
        str4:=Str1[j];
        Writeln(MothodFile,str4);
      End;
    CloseFile(MothodFile);End;end.
      

  4.   

    看你小子太小气了,一分都不给,不过做个好人,下次大方点...
    procedure TForm1.Button1Click(Sender: TObject);
    var
      F         : TextFile;
      Str       : string;
      FileName  : string;
    begin
      AssignFile(F,FileName);
      if FileExists(FileName) then
        Append(F)
      else
        ReWrite(F);
      WriteLn(Str);
      CloseFile(F);
    end;