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/