filecopy文件拷贝

来自中国的朋友,欢迎您在本版面使用中文讨论问题。请注意,如果您想得到不懂中文的人的帮助,请同时提供英文译文。
Help and discussions in Chinese.
Post Reply
liqi98136
Posts: 53
Joined: Sun Jan 17, 2010 11:37 pm

filecopy文件拷贝

Post by liqi98136 »

Code: Select all

import "ecere"

class FiletoFile : Application
{
   void Main()
   {
      File f1 = FileOpen(argv[1],read);
      File f2 = FileOpen(argv[2],write);
      char ch;
      if(f1)
      {
         f1.Get(ch);

         while(ch)
         {
            f2.Putc(ch);
            f1.Get(ch);
         }
      }else
      {
         printf("open file failed\n");
      }

      delete f2;
      delete f1; 
      
       
   }
} 
jerome
Site Admin
Posts: 608
Joined: Sat Jan 16, 2010 11:16 pm

Re: filecopy文件拷贝

Post by jerome »

I recently introduced a 'CopyTo' method in the File class.
It takes an outputFileName, and you just go:

Code: Select all

File f = FileOpen("sourceFile.txt", read);
if(f)
{
   f.CopyTo("destFile.txt");
   delete f;
}
Since it's not in any Ecere package yet, here's the source code for it:

Code: Select all

   bool CopyTo(char * outputFileName)
   {
      bool result = false;
      File f = FileOpen(outputFileName, write);
      if(f)
      {
         byte buffer[65536];
 
         result = true;
         Seek(0, start);
         while(!Eof())
         {
            uint count = Read(buffer, 1, sizeof(buffer));
            if(count && !f.Write(buffer, 1, count))
            {
               result = false;
               break;
            }
         }
      }
      Seek(0, start);
      return result;
   }
As you can see, this uses a buffer and should be significantly faster. There is also the 'BufferedFile' class (derived from 'File') which implements automatic buffering on both reading and writing, and would give you more performance on your per character implementation. However it's best to read and write bunches of bytes at once, as above.

Cheers,

Jerome
Post Reply