I am trying to copy a file from one location to another( in a device) using C++/Qt
I tried QFile::copy("path1/file","path2");
I want to copy the file in path1 to path2. path2 does not have the file.
I just want to know if this is the right way because the above code does not seem to work.
Also, should I do a file open before I try to copy? Need help!
QFile is an I/O device for reading and writing text and binary files and resources. A QFile may be used by itself or, more conveniently, with a QTextStream or QDataStream. The file name is usually passed in the constructor, but it can be set at any time using setFileName (). QFile expects the file separator to be '/' regardless of operating system.
This function was introduced in Qt 6.0. Constructs a new file object with the given parent to represent the file with the specified name. Constructs a new file object with the given parent. Constructs a new file object to represent the file with the given name. This function was introduced in Qt 6.0.
Just an update to this answer from the QT 4.8 documentation: "QFile expects the file separator to be / regardless of operating system. The use of other separators (e.g., '\') is not supported." .. so make sure to use forward slashes / for file paths. Source: doc.qt.io/qt-4.8/qfile.html
A QFile may be used by itself or, more conveniently, with a QTextStream or QDataStream. The file name is usually passed in the constructor, but it can be set at any time using setFileName (). QFile expects the file separator to be '/' regardless of operating system.
If you want to copy path1/file
into path2
with the same file name, you'll want to do:
QFile::copy("path1/file", "path2/file");
Copy allows you to change the name of the file. Example:
QFile::copy("path1/file1", "path1/file2");
Which is why you need to include the file name both times. Also, it is not necessary to open the file first. And to answer the title question, it copies the file. QFile::rename()
moves the contents.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With