Unable to copy a file from one directory to another
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.
Code:
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())) 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
Re: Unable to copy a file from one directory to another
Please provide a minimal compilable example reproducing the problem.
Re: Unable to copy a file from one directory to another
Code:
#include "mainwindow.h"
#include <QApplication>
#include <QDir>
int main(int argc, char *argv[])
{
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
Re: Unable to copy a file from one directory to another
Line 34 tries to copy srcfile, which refers to the path dbdestinationfilepath, to dbdestinationfilepath. That seems unlikely to be productive.
Re: Unable to copy a file from one directory to another
Thats fine, it has to be srcfile.copy(dbsrcfilepath);
Firstly if true case is not working.
Re: Unable to copy a file from one directory to another
Quote:
Originally Posted by
ejoshva
Thats fine
Why is that fine? You cannot copy the file over itself.
Re: Unable to copy a file from one directory to another
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.
Re: Unable to copy a file from one directory to another
Quote:
Originally Posted by
ejoshva
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.
Re: Unable to copy a file from one directory to another
Quote:
Originally Posted by
ejoshva
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,
_
Re: Unable to copy a file from one directory to another
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.
Re: Unable to copy a file from one directory to another
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.
Re: Unable to copy a file from one directory to another
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
Code:
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();
}
Re: Unable to copy a file from one directory to another
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:
Code:
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.
Re: Unable to copy a file from one directory to another
Hi ,
Could you please share me the working code for copy a file from one directory to another.
Thank you.
Quote:
Originally Posted by
ejoshva
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
Code:
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();
}
Re: Unable to copy a file from one directory to another
Quote:
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?
Re: Unable to copy a file from one directory to another
Apart from all the rest this was the initial problem
Code:
qDebug()<<destifile.readAll().length();
success &= srcfile.write( destifile.readAll() ) >= 0;
The file was read in completely during the qDebug() statement...