qt 复制文件到指定路径
时间: 2023-11-27 17:02:52 浏览: 81
可以使用QFile类和QFileInfo类来复制文件到指定路径。以下是一个简单的示例代码:
```c++
#include <QFile>
#include <QFileInfo>
int main()
{
QString sourceFilePath = "/path/to/source/file";
QString destinationFilePath = "/path/to/destination/file";
// 创建一个QFile对象并打开源文件
QFile sourceFile(sourceFilePath);
if (!sourceFile.open(QIODevice::ReadOnly)) {
qDebug() << "Failed to open source file";
return -1;
}
// 创建一个QFile对象并打开目标文件
QFile destinationFile(destinationFilePath);
if (!destinationFile.open(QIODevice::WriteOnly)) {
qDebug() << "Failed to open destination file";
return -1;
}
// 复制源文件到目标文件
QByteArray data = sourceFile.readAll();
destinationFile.write(data);
// 关闭文件
sourceFile.close();
destinationFile.close();
return 0;
}
```
可以根据实际需求对代码进行修改,例如添加错误处理和进度提示等。
阅读全文
相关推荐
















