Supprimer un fichier en C++

kenzy -  
tafiscobar Messages postés 1281 Statut Contributeur -
Bonjour pouvez vous m'aidez !!!
je voudrais programmé une application me permettant de supprimer un fichier dans un répertoire (avec le chemin que je donne en paramétre) enC++.
Merci de m'aidez
ps: si cela est trop compliké ,donnez moi au moins un lien ou je puisse avoir un faq que je puisse voir svp.
Merci bcp

1 réponse

  1. tafiscobar Messages postés 1281 Statut Contributeur 177
     
    ce n'est pas compliqé. Il te faut juste connaitre l'api qui manipule le systéme de fichiers. Voici un exemple en C++ avec .Net (piqué sur le msdn):

    #using <mscorlib.dll>
    
    using namespace System;
    using namespace System::IO;
    
    int main() {
        String* path = S"c:\\temp\\MyTest.txt";
        try {
            StreamWriter* sw = File::CreateText(path);
            if (sw) __try_cast<IDisposable*>(sw)->Dispose();
            String* path2 = String::Concat(path, S"temp");
    
            // Ensure that the target does not exist.
            File::Delete(path2);
    
            // Copy the file.
            File::Copy(path, path2);
            Console::WriteLine(S"{0} was copied to {1}.", path, path2);
    
            // Delete the newly created file.
            File::Delete(path2);
            Console::WriteLine(S"{0} was successfully deleted.", path2);
        } catch (Exception* e) {
            Console::WriteLine(S"The process failed: {0}", e);
        }
    }
    0