【问题标题】:C++ Transfer Files From Qt to External USB DriveC++ 将文件从 Qt 传输到外部 USB 驱动器
【发布时间】:2017-09-02 11:55:27
【问题描述】:

我是 Qt 新手,在将所有文件从本地计算机的特定路径传输到外部 USB 驱动器时需要帮助。

【问题讨论】:

标签: c++ qt file-copying


【解决方案1】:

复制单个文件

您可以使用QFile::copy

QFile::copy(srcPath, dstPath);

注意:此功能不会覆盖文件,因此如果存在之前的文件,则必须删除它们:

if (QFile::exist(dstPath)) QFile::remove(dstPath);

如果您需要显示一个用户界面来获取源路径和目标路径,您可以使用QFileDialog 的方法来做到这一点。示例:

bool copyFiles() {
  const QString srcPath = QFileDialog::getOpenFileName(this, "Source file", "",
    "All files (*.*)");
  if (srcPath.isNull()) return false; // QFileDialog dialogs return null if user canceled

  const QString dstPath = QFileDialog::getSaveFileName(this, "Destination file", "",
    "All files (*.*)"); // it asks the user for overwriting existing files
  if (dstPath.isNull()) return false;

  if (QFile::exist(dstPath))
    if (!QFile::remove(dstPath)) return false; // couldn't delete file
      // probably write-protected or insufficient privileges

  return QFile::copy(srcPath, dstPath);
}

复制目录的全部内容

我将答案扩展到srcPath 是一个目录的情况。它必须手动和递归地完成。这是执行此操作的代码,为简单起见没有错误检查。您必须负责选择正确的方法(请查看QFileInfo::isFile 了解一些想法。

void recursiveCopy(const QString& srcPath, const QString& dstPath) {
  QDir().mkpath(dstPath); // be sure path exists

  const QDir srcDir(srcPath);
  Q_FOREACH (const auto& dirName, srcDir.entryList(QStringList(), QDir::Dirs | QDir::NoDotAndDotDot, QDir::Name)) {
    recursiveCopy(srcPath + "/" + dirName, dstPath + "/" + dirName);
  }

  Q_FOREACH (const auto& fileName, srcDir.entryList(QStringList(), QDir::Files, QDir::Name)) {
    QFile::copy(srcPath + "/" + fileName, dstPath + "/" + fileName);
  }
}

如需查询目录,可使用QFileDialog::getExistingDirectory

最后的评论

这两种方法都假定srcPath 存在。如果您使用QFileDialog 方法,它很可能存在(很可能是因为它不是原子操作,并且目录或文件可能会在对话框和复制操作之间被删除或重命名,但这是一个不同的问题) .

【讨论】:

  • 感谢您的帮助。我会用它来解决我的问题。
  • 很高兴为您提供帮助,如果它解决了您的问题,如果您需要进一步的帮助,请告诉我
  • Idk 我仍然坚持这个问题。我有文件的静态路径以及像 QString srcPath = dir.relativeFilePath("/home/untitled"); 这样的外部驱动器。因为静态的东西,我没有使用QFileDialog。我只是在头文件中声明源和目标文件夹并在 main.cpp 上调用 QFile::copy(srcPath, dstPath); 这个。
  • QDir::relativeFilePath 顾名思义,考虑到相对于当前目录的路径。只需使用QString srcPath = "/home/untitled";
  • 我也添加了一个方法来制作递归副本,看一下(如果有错误,我打字很快,无法验证所有案例)。跨度>
【解决方案2】:

我已经解决了QStorageInfo::mountedVolumes() 的问题,它返回连接到机器的设备列表。但除了 Pendrive 或 HDD 之外,它们都没有名字。所以(!(storage.name()).isEmpty())) 只会返回那些设备的路径。

QString location;
QString path1= "/Report/1.txt";
QString locationoffolder="/Report";

foreach (const QStorageInfo &storage, QStorageInfo::mountedVolumes()) {
    if (storage.isValid() && storage.isReady() && (!(storage.name()).isEmpty())) {
        if (!storage.isReadOnly()) {

           qDebug() << "path:" << storage.rootPath();
           //WILL CREATE A FILE IN A BUILD FOLDER
           location = storage.rootPath();
           QString srcPath = "writable.txt";
           //PATH OF THE FOLDER IN PENDRIVE
           QString destPath = location+path1;
           QString folderdir = location+locationoffolder;
           //IF FOLDER IS NOT IN PENDRIVE THEN IT WILL CREATE A FOLDER NAME REPORT
           QDir dir(folderdir);
           if(!dir.exists()){
              dir.mkpath(".");
           }

           qDebug() << "Usbpath:" <<destPath;
           if (QFile::exists(destPath)) QFile::remove(destPath);
           QFile::copy(srcPath,destPath);
           qDebug("copied");

        }
    }
}

由于我的要求,我不得不在 USB 中创建一个文件夹,并且我为这些文件指定了一个静态名称。然后我只是将数据从本地机器的文件复制到我在 QFile::copy(srcPath, dstPath) 的帮助下在 USB 中创建的文件中。我希望它会对某人有所帮助。

【讨论】:

    猜你喜欢
    • 2023-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-27
    • 2014-02-13
    • 2019-09-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多