0
QString source = "E:/source/tty.txt";
QString dest = "E:/Destination";

bool status =QFile::copy(source,dest); // status = false

It doesn't copy the source file to destination file. Why?

secretgenes
  • 1,291
  • 1
  • 19
  • 39

2 Answers2

2

you didn't define file name for destination directory.

Try this:

QString source = "E:/source/tty.txt";
QString dest = "E:/Destination/tty.txt";

bool status = QFile::copy(source,dest);

More info: This is an overloaded function. Copies the file fileName to newName. Returns true if successful; otherwise returns false.

If a file with the name newName already exists, copy() returns false (i.e., QFile will not overwrite it).

http://doc.qt.io/qt-5/qfile.html#copy

Farhad
  • 4,119
  • 8
  • 43
  • 66
0

By using a QFile instance and method QFile::copy(const QString &newName), you could find out the error code by using QFileDevice::error() if copy returns false.

Something like this:

QFile source("E:/source/tty.txt");
QString dest = "E:/Destination/tty.txt";
if (!source.copy(dest))
{
     qDebug() << "File error" << source.error();  
}
Jens
  • 6,173
  • 2
  • 24
  • 43
  • I've tried this also. I think there is something wrong with copy function , something is happening inside copy function that stops it from copying to destination – secretgenes Aug 29 '17 at 10:04