qt - 将整个数组写入 QFile

标签 qt qfile

QFile::write() documentation说:

Writes at most maxSize bytes of data from data to the device. Returns the number of bytes that were actually written, or -1 if an error occurred.

(是的,这就是整个文档 - 对于 Qt 来说异常糟糕。)

这似乎意味着它不写入您传递的所有数据是很常见的。这是否意味着我应该在循环中调用 write(),传入剩余的未写入数据,直到其完全写入为止?

如果是这样,那似乎很痛苦。 Qt 中是否有一些方便的功能可以为我做到这一点?

最佳答案

This seems to imply that it is common for it not to write all of the data you pass

常见,不,但有可能,我们将在下面看到。

Is there some convenience function in Qt that will do it for me?

如果您正在编写整个文件,更好的方法是使用 QSaveFile ,用于“安全写入文件”。

正如QSaveFile文档中所述:

QSaveFile automatically detects errors while writing, such as the full partition situation, where write() cannot write all the bytes.

因此,完整分区可能是导致 QIODevice::write 无法将数据完全写入磁盘的一个实例。磁盘故障、在写入期间移除可移植存储设备或网络驱动器也可能导致仅写入部分数据。

关于qt - 将整个数组写入 QFile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43997816/

相关文章:

python - 为什么我不能使用 QtSql (PyQT) 执行 SHOW PROCESSLIST?

c++ - 比较字符串时 QtTest 失败,即使它们相同? C++

c++ - 返回 QFile::copy from web

c++ - 如何将由我的类的对象组成的 QVector 的数据写入文件?

c++ - Qt C++ : Line edit accept only alphanumeric characters, 破折号和下划线

c++ - 我应该在模板类头文件中包含我需要的一切吗?

c++ - QFile::remove 不删除文件?

c++ - 在Qt中修改文件的lastModified/lastAccessed/created日期

windows - QFile::copy 返回真,即使在 Windows 中复制失败

c++ - QString::from Char Array 给出了奇怪的字符