我想要在程序中实现这样的功能:
1、在D:下新建一个空的TXT文件:DTest.txt
2、假如D:下有一个包含一些字符的文件:STest.txt,那么,我要把STest.txt中的内容复制到DTest.txt中
请问怎么样实现这个功能,谢谢!
先给50分,不够再加!

解决方案 »

  1.   


    procedure TForm1.btn1Click(Sender: TObject);
    var
      F: TextFile;
    begin
      // 有STest.txt则拷贝
      if FileExists('d:\STest.txt') then begin
        CopyFile('d:\STest.txt', 'd:\DTest.txt', false);
        Exit;
      end;
      //没有则创建新的
      AssignFile(F, 'd:\DTest.txt');
      Rewrite(F);
      CloseFile(F);
    end;
      

  2.   

    楼主是
    “我要把STest.txt中的内容复制到DTest.txt中”
    CopyFile会覆盖已存在的文件啊
      

  3.   

    我只是想把STest的内容放到DTest里面,而不是复制整个文件,因为我的DTest格式是UTF,而STest是ANSI,这样复制整个文件,DTest的格式也变成ANSI了,请问哪里有单单复制内容的函数???
      

  4.   

    你那个已经不是"简单"的内容COPY
    而是编码转换
    自己用编码处理函数
      

  5.   

    我把ini檔裏的資料copy到txt可以實現;把txt檔裏的資料copy到txt亦能實現啊!
    請問你說的UTF和ANSI是可編輯文件嗎?
      

  6.   

    var
    DFile: TStringList;  DFile := TStringList.Create;
      try
        if not FileExists('d:\DTest.txt') then
        begin
          DFile.SaveToFile('d:\DTest.txt');
          //新建一个空的文件
        end;
        if FileExists('d:\STest.txt') then
        begin
          DFile.LoadFromFile('d:\STest.txt');
          DFile.SaveToFile('d:\DTest.txt');
        end;
      finally
        DFile.Free;
      end;
    这么简单抢分,有好多方法。
      

  7.   

    var
      SaveFile:string;
      NewFile,OldFile:TFileStream;
    begin
      SaveDialog.InitialDir :=ExtractFilePath(Application.ExeName)+'conf\10001\';
      if SaveDialog.Execute then
      begin
        SaveFile :=SaveDialog.FileName;
        try
          if FileExists(SaveFile) then
          begin
            if Application.MessageBox('您输入的文件名已经存在,确定要覆盖吗?','提示',MB_YESNO)=ID_YES then
              CopyFile(PChar(FileName),PChar(SaveFile),False);// False 覆盖原来已经存在的文件
          end else
          begin
            OldFile:=TFileStream.Create(FileName,fmOpenRead);
            try
              NewFile:=TFileStream.Create(SaveFile,fmOpenWrite or fmCreate);
              try
                NewFile.CopyFrom(OldFile,OldFile.Size);
              finally
                NewFile.Free;
              end;
            finally
              OldFile.Free;
            end;
          end;
        except
          ShowMessage('保存失败!');
        end;
      end;这是我以前做过的一个程序,实现的是另存为的功能:如果文件中没有要保存的文件名,就创建一个,如果有的话就把原来的文件覆盖掉。
      

  8.   

    “我只是想把STest的内容放到DTest里面,而不是复制整个文件,因为我的DTest格式是UTF,而STest是ANSI,这样复制整个文件,DTest的格式也变成ANSI了,请问哪里有单单复制内容的函数???”------------
    请看清楚楼主的实际需求意图,谢谢!并请楼主回复我的问题“請問你說的UTF和ANSI是可編輯文件嗎?”
    如果没有时间请直接mail给我 :[email protected]
      

  9.   

    - -!- -!- -!- -!- -!- -!- -!我只看了标题 怎么中间又变了 狂晕
    ansitoutf8 转换
    utf8toansi var 
    SFile,DFile:   TStrings;      DFile   :=   TStringList.Create; 
        SFile   :=   TStringList.Create; 
        try 
           if FileExists('d:\DTest.txt') then 
           begin 
             DFile.LoadFromFile('d:\DTest.txt');
            end; 
           if FileExists('d:\STest.txt')   then 
           begin 
             SFile.LoadFromFile('d:\STest.txt');
             if ansitoutf8(SFile.Text) <> '' then  //ansitoutf8注意SFile.Text如果不正确会返回空
              begin 
               SFile.Text := ansitoutf8(SFile.Text);
               DFile.AddStrings(SFile);
               DFile.SaveToFile('d:\DTest.txt'); 
             end;
            end; 
        finally 
            DFile.Free; 
            SFile.Free; 
        end; 
      

  10.   

    非常感谢CYDong0423给我提供了ansitoutf8这个函数以及相应解决方法,使我的问题得到解决。
    由于本贴分数不多而答复人却不少,所以分给得少请你见谅。
    (悄悄告诉你,你可以到
    http://topic.csdn.net/u/20071210/10/199bb332-a112-493a-8049-3e7bab781450.html
    去答复我的另一个相关贴子,我在那边给你多些分。)