Kleine Filecopy-Routine



  • Hallo,

    möchte hier mal eine kleine Funktion vorstellen, um Dateien zu kopieren:

    [cpp]
    void __fastcall FileCopy(String Source, String Destination)
    {  
      TFileStream *src  = new TFileStream(Source,fmOpenRead);
      TFileStream *dest = new TFileStream(Destination,fmCreate);
      dest->CopyFrom(src,0);
      delete dest;
      delete src;
    }
    [/cpp]
    

    Gruß Burkhard



  • Hallo

    normalerweise würde ich dafür aber WinAPI-Funktionen wie CopyFile nehmen.

    bis bald
    akari



  • Hallo,

    CopyFile dürfte hier wirklich am Schnellsten sein.

    @Burkhi
    Übergib Strings lieber als konstante Referenz. Da sparst du Kopien. Außerdem sollte um die news noch ein try/catch-Block kommen um Fehler abzufangen.

    Hier mal noch aus Spaß die Standard-C++ Variante

    bool FileCopy(const std::string& Source, const std::string& Destination)
    {  
      ifstream in(Source.c_str());
      ofstream out(Destination.c_str());
      if( !in.is_open() || !out.is_open() ) return false;
      out << in.rdbuf();
      return true;
    }
    


  • akari schrieb:

    Hallo

    normalerweise würde ich dafür aber WinAPI-Funktionen wie CopyFile nehmen.

    bis bald
    akari

    @akari:
    😃 Hast gewonnen, das geht natürlich noch schneller (und kürzer):D 😉

    @Braustein:
    Deine Variante gefällt sogar mir besser, weils da keine new und delete-Operationen gibt. Und bei meiner Variante wär ein Try und Catch-Block besser gewesen, das stimmt. 😉


Anmelden zum Antworten