var
  mytextfile : TextFile ;
  s : string ;
  i : integer ;
begin
  AssignFile(mytextFile,'MyTxtFile.txt') ;//创建,有则覆盖
  ReWrite(mytextfile) ;//以写文件方式打开文件
  try
       s := 'abcd ' ;
       writeln(mytextfile,s,i) ;//写入ABCD
  finally
     closefile(mytextfile) ;//关闭文件
  end;end;

解决方案 »

  1.   

    var f:textfile; SearchRec: TSearchRec; str:string;
    begin
                  //找文件,找到该文件中止
                   FindFirst('val.txt', faAnyFile, SearchRec);
                   if SearchRec.Name='val.txt' then abort;
     
                  //找不到该文件开始写文件
                     AssignFile(F,'val.txt');
                     Rewrite(F);
                     str:='abcd';
                     Writeln(F,str);
                     append(f);
                     closefile(f);
    end;
      

  2.   

    To:zds0707(周) ,atang(阿汤):
    刚才我试过了,有两个问题:
       1.加入该文件已经存在,则必须向里面添加内容,而不是先清空文件内容。
       2.我在写该文件的时候,别的程序可以打开该文件并进行读写。我的要求是
         必须在写的过程中别的程序不能访问该文件。
      

  3.   

    要独占,把那个文件删除了,写的时候写到一个临时文件里,等写完内容在把临时文件Copy或者Rename就可以了
      

  4.   

    有这么麻烦吗?
    TFileStream中有一个标志:
     fmShareExclusive,不知道怎么用到TextFile
      

  5.   

    实在不行,我只能用Windows API函数OpenFile了。有人会吗,我再加50分!!!