Copy File from One folder to other Folder.New to Qt



  • files.h

    #ifndef FILES_H
    #define FILES_H
    
    #include <QObject>
    
    class files
    {
    public:
        files();
        QString srcPath = "/home/roopkumar/FileTransfer/1.txt";
        QString dstPath = "/media/roopkumar/Downloads";
        bool copyfile();
    };
    
    #endif // FILES_H
    
    
    include "files.h"
    #include<QFile>
    
    files::files()
    {
    
    }
    
     bool files::copyfile(){
         if (QFile::exists(dstPath))
         {
             QFile::remove("home/roopkumar/Downloads/.txt");
         }
    
        QFile sourceFile( srcPath);
        QFile destFile( dstPath );
        bool success = true;
        success &= sourceFile.open( QFile::ReadOnly );
        success &= destFile.open( QFile::WriteOnly | QFile::Truncate );
        success &= destFile.write( sourceFile.readAll() ) >= 0;
         QFile::copy(srcPath,dstPath);
        sourceFile.close();
        destFile.close();
    
        return success;
    }
    **files.cpp**
    

    main.cpp

     files *f = new files();
        f->copyfile();
    

    Error : QIODevice::write (QFile, "/media/roopkumar/Downloads/2.txt"): device not open
    where am i doing mistake help me with that.



  • dstPath is a folder, not a file.
    You don't need to open and read/write them QFile::copy is enough. all you need is:

    QString dstPath = "/media/roopkumar/Downloads/2.txt"
    if(QFile::exists(dstPath))
    QFile::remove(dstPath);
    if(!QFile::copy("/home/roopkumar/FileTransfer/1.txt",dstPath )) Q_ASSERT(false);
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.