Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will QFile::copy create create a copy of the file or move the contents from one file to another?

Tags:

c++

qfile

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!

like image 682
user1065969 Avatar asked May 11 '12 18:05

user1065969


People also ask

What is qfile in QForms?

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.

What does the CreateFile () function do in QT 6?

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.

Is it possible to use other file separators in qfile?

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

How to use qfile in qtextstream?

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.


1 Answers

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.

like image 180
Joel Rondeau Avatar answered Oct 01 '22 09:10

Joel Rondeau