在 QT 中复制两个二进制文件的最佳方法是什么
What's the best way to copy two binary files in QT
我想将一个二进制文件复制到另一个二进制文件。
我唯一的限制是复制必须通过 QFile
进行(因为我已经重载了一些内部方法并且我需要它们 运行)。
我写了一个天真的方法来解决,但写得慢:
QFile * write_to = new QFile("myfile.bin");
if(write_to->open(QFile::WriteOnly))
{
QFile read_from("my_outher_bin.bin");
if(read_from.open(QIODevice::ReadOnly))
{
QDataStream write_data(write_to);
QDataStream read_data(&read_from);
while(write_to->size() < read_from.size())
write_data << read_data;
}
}
最有效的方法是什么?
一种简单而安全的方法,使用 STD 库,std::ifstream 是一个不错的选择:
std::ifstream src("file.txt", std::ios::binary);
std::ofstream dst("to_file.txt", std::ios::binary);
dst << src.rdbuf();
如果您使用的是 C++17,则可以使用 <filesystem> extension and use the fs::copy_file 函数。
fs::copy_file("file.txt", "to_file.txt");
我想将一个二进制文件复制到另一个二进制文件。
我唯一的限制是复制必须通过 QFile
进行(因为我已经重载了一些内部方法并且我需要它们 运行)。
我写了一个天真的方法来解决,但写得慢:
QFile * write_to = new QFile("myfile.bin");
if(write_to->open(QFile::WriteOnly))
{
QFile read_from("my_outher_bin.bin");
if(read_from.open(QIODevice::ReadOnly))
{
QDataStream write_data(write_to);
QDataStream read_data(&read_from);
while(write_to->size() < read_from.size())
write_data << read_data;
}
}
最有效的方法是什么?
一种简单而安全的方法,使用 STD 库,std::ifstream 是一个不错的选择:
std::ifstream src("file.txt", std::ios::binary);
std::ofstream dst("to_file.txt", std::ios::binary);
dst << src.rdbuf();
如果您使用的是 C++17,则可以使用 <filesystem> extension and use the fs::copy_file 函数。
fs::copy_file("file.txt", "to_file.txt");