C++ Qt - 将文件从一个目录复制到另一个目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19928216/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Qt - copy a file from one directory to another
提问by joki
I am using QT, I am not able to find out how to copy a file from one directory to another? How can I achieve this?
我正在使用 QT,我不知道如何将文件从一个目录复制到另一个目录?我怎样才能做到这一点?
回答by Stephan
回答by Benjamin Lucidarme
If destination file exist, QFile::copy
will not work. The solution is to verify if destination file exist, then delete it:
如果目标文件存在,QFile::copy
将不起作用。解决方案是验证目标文件是否存在,然后将其删除:
if (QFile::exists("/path/copy-of-file"))
{
QFile::remove("/path/copy-of-file");
}
QFile::copy("/path/file", "/path/copy-of-file");
回答by Iron Man
The following code works in windows for specified reasons. This will set the path to specified drive and create the folder you created in Under UI Mode. Then copies the file from source to destination. Here the source is installation directory contained some files which are used for plotting curves. this file are not modified by users. They just use it.
以下代码出于特定原因在 Windows 中工作。这将设置指定驱动器的路径并创建您在用户界面模式下创建的文件夹。然后将文件从源复制到目标。这里的源是安装目录,其中包含一些用于绘制曲线的文件。该文件不被用户修改。他们只是使用它。
hence this works as copy from installation directory to specified folder
因此这可以作为从安装目录到指定文件夹的复制
void MainWindow::on_pushButton_2_clicked()
{
QString str5 = ui->lineEdit->text();
QString src = "."; QString setpath;
QDir dir(src);
if(!dir.exists()){
return;
}
dir.cdUp();
//dir.cdUp();
setpath = "E://";
dir.setPath(setpath);
QString dst_path = str5 + QDir::separator() ;
dir.mkpath(dst_path);
dir.cd(dst_path);
QString filename = "gnu.plt";
QString filename2 = "Load curve.plt";
QString filename3 = "tube temp.plt";
QFile file(filename);
QFile file1(filename2);
QFile file2(filename3);
file.copy(src+QDir::separator()+filename, setpath+QDir::separator()+str5+QDir::separator()+filename);
file1.copy(src+QDir::separator()+filename2, setpath+QDir::separator()+str5+QDir::separator()+filename2);
file2.copy(src+QDir::separator()+filename3, setpath+QDir::separator()+str5+QDir::separator()+filename3);
}