PDA

View Full Version : Unable to copy a file from one directory to another



ejoshva
19th March 2015, 11:26
I am trying to copy a file from one directory to another

Tried QFile::copy(). The file is copied but the size is zero Kb

Tried placing a dummy file and then replacing with original

But still the size is 0 Kb.




QDir qd;
QFile srcfile;
QFile destifile;

QString dbsrcfilepath = qd.currentPath().append("/learnOn1.sqlite");
dbsrcfilepath = QDir::toNativeSeparators(dbsrcfilepath);
// dbsrcfilepath : "/Users/user/build-learnOn-Desktop_Qt_5_4_0_clang_64bit-Debug/learnOn.app/Contents/MacOS/learnOn1.sqlite"
QString libpath = qd.homePath() + QDir::separator()+"learnOnContent" + QDir::separator();
if(!(QDir(libpath).exists()))
QDir().mkdir(libpath);
QString dbdestinationfilepath = libpath.append("learnOn.sqlite");
//dbdestinationfilepath : "/Users/user/learnOnContent/learnOn.sqlite"

dbdestinationfilepath = QDir::toNativeSeparators(dbdestinationfilepath);

srcfile.setFileName(dbdestinationfilepath);
destifile.setFileName(dbsrcfilepath);
if(!srcfile.exists())
{
bool success = true;
success &= destifile.open( QFile::ReadOnly );
success &= srcfile.open( QFile::WriteOnly | QFile::Truncate );
qDebug()<<destifile.readAll().length();
success &= srcfile.write( destifile.readAll() ) >= 0;
destifile.close();
srcfile.close();
}
else
{
srcfile.copy(dbdestinationfilepath);
}



The copy is not proper. File size is always 0.

Kindly help to resolve this issue.

Thanks in advance

wysota
19th March 2015, 11:28
Please provide a minimal compilable example reproducing the problem.

ejoshva
19th March 2015, 11:50
#include "mainwindow.h"
#include <QApplication>
#include <QDir>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);

QDir qd;
QFile srcfile;
QFile destifile;

QString dbsrcfilepath = "/Users/user/build-learnOn-Desktop_Qt_5_4_0_clang_64bit-Debug/learnOn.app/Contents/MacOS/learnOn1.sqlite";
dbsrcfilepath = QDir::toNativeSeparators(dbsrcfilepath);
destifile.setFileName(dbsrcfilepath);
if(!(QDir("/Users/user/learnOnContent/").exists()))
QDir().mkdir("/Users/user/learnOnContent/");

QString dbdestinationfilepath = "/Users/user/learnOnContent/learnOn.sqlite";
dbdestinationfilepath = QDir::toNativeSeparators(dbdestinationfilepath);
srcfile.setFileName(dbdestinationfilepath);

if(!srcfile.exists())
{
bool success = true;
success &= destifile.open( QFile::ReadOnly );
success &= srcfile.open( QFile::WriteOnly | QFile::Truncate );

success &= srcfile.write( destifile.readAll() ) >= 0;
destifile.close();
srcfile.close();
}
else
{
srcfile.copy(dbdestinationfilepath);
}

return a.exec();
}



copying from dbsrcfilepath to dbdestinationfilepath

ChrisW67
19th March 2015, 12:11
Line 34 tries to copy srcfile, which refers to the path dbdestinationfilepath, to dbdestinationfilepath. That seems unlikely to be productive.

ejoshva
19th March 2015, 12:46
Thats fine, it has to be srcfile.copy(dbsrcfilepath);

Firstly if true case is not working.

wysota
19th March 2015, 13:15
Thats fine
Why is that fine? You cannot copy the file over itself.

ejoshva
19th March 2015, 13:53
What I meant was 'else' case is not the problem as it's written on the fly and I can correct it.

I wanted to resolve the issue in the if success case first.

wysota
19th March 2015, 14:02
What I meant was 'else' case is not the problem as it's written on the fly and I can correct it.

I wanted to resolve the issue in the if success case first.

I have no idea what you mean, sorry.

anda_skoa
19th March 2015, 14:45
I wanted to resolve the issue in the if success case first.
You solved that yourself then, because the "then" case does not use QFile::copy().

Cheers,
_

ejoshva
20th March 2015, 04:33
The else part was a mistake and I have corrected it as was pointed out.

The priority is the then case.

That's what I meant

Added after 26 minutes:

I removed the then clause and executed only with the else.
Still it was not getting copied.
File size 0Kb only.

wysota
20th March 2015, 12:05
I suggest you actually check the results of opening the source file before trying to read anything from it. Also if the file is large, there is a good chance your process will crash.

ejoshva
25th March 2015, 05:04
Got the issue resolved. Thanks for your inputs

issue was with the write function.
QFILE::(const char *data, qint64 len)
Upon using this function , the write is successful. Also data.size() is to be used and not data.length()

The working code is below



if(srcfile.exists())
{
bool success = true;
success &= srcfile.open( QFile::ReadOnly );
success &= destifile.open( QFile::WriteOnly | QFile::Truncate );
success &= srcfile.write( destifile.readAll(),destifile.size()) >= 0;
destifile.close();
srcfile.close();
}

jefftee
25th March 2015, 06:13
If you are copying large files, that's not a very good idea to destifile.readAll(). Why aren't you using QFile::copy() again? It seems trivial to:



QFile destifile(dbdestinationfilepath);
if (destifile.exists())
{
destifile.remove();
}
srcfile.copy(dbdestinationfilepath);

QFile::copy() won't overwrite a destination file, which is why I checked if it exists and remove it if it already exists. In the original code you posted, you are treating source and destination backwards IMHO... i.e. your open was truncating the source file which I doubt is what you actually intended.

QFile::copy works, just make sure the destination file doesn't already exist and you can simplify your code.

Rajesh21
12th January 2019, 12:53
Hi ,

Could you please share me the working code for copy a file from one directory to another.
Thank you.







Got the issue resolved. Thanks for your inputs

issue was with the write function.
QFILE::(const char *data, qint64 len)
Upon using this function , the write is successful. Also data.size() is to be used and not data.length()

The working code is below



if(srcfile.exists())
{
bool success = true;
success &= srcfile.open( QFile::ReadOnly );
success &= destifile.open( QFile::WriteOnly | QFile::Truncate );
success &= srcfile.write( destifile.readAll(),destifile.size()) >= 0;
destifile.close();
srcfile.close();
}

d_stranz
12th January 2019, 17:06
Could you please share me the working code for copy a file from one directory to another.

The code posted by jefftee does exactly that. Did you try it before making your post?

ChristianEhrlicher
13th January 2019, 15:59
Apart from all the rest this was the initial problem


qDebug()<<destifile.readAll().length();
success &= srcfile.write( destifile.readAll() ) >= 0;

The file was read in completely during the qDebug() statement...