Ecere SDK/eC Forums
http://www.ecere.com/forums/
Print view

filecopy文件拷贝
http://www.ecere.com/forums/viewtopic.php?f=30&t=46
Page 1 of 1
Author:  liqi98136 [ Wed Mar 10, 2010 9:24 am ]
Post subject:  filecopy文件拷贝

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; 
      
       
   }
} 
Author:  jerome [ Wed Mar 17, 2010 6:29 pm ]
Post subject:  Re: filecopy文件拷贝

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
All times are UTC-05:00 Page 1 of 1