1
votes

I need write n bytes to file and I have QTemporaryFile, how should I write these bytes?

I read QIODevice::write documentation:

qint64 QIODevice::write(const char *data, qint64 maxSize) 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.

so looks like I need cycle to write bytes, because of there is no grantee that it writes all bytes, it may return control after writting k bytes, where k < n.

I can create QDataStream from TemporaryFile, but QDataStream::writeRawData function has the same restriction:

int QDataStream::writeRawData(const char *s, int len) Writes len bytes from s to the stream. Returns the number of bytes actually written, or -1 on error. The data is not encoded.

so there is no function in Qt that write exactly n bytes or return error?

1
so there is no function in Qt that write exactly n bytes or return error well, that exactly what QIODevice::write do with error condition as k != nAlexey Andronov
@AlexeyAndronov but documentation says that on error it returns -1, about k <= n, there is no mention that on error it returns k >= 0 and k != n, so may be in some cases it is normaluser1244932
my bad, didn't read the doc properly. Then, condition is k == -1 || k != n where k is return value of write. There's no need of such a simple function in a library when you can write a wrapper yourself :)Alexey Andronov

1 Answers

0
votes

How do you propose undoing the write of some bytes, in the case where there is no error but fewer than n bytes were written? Especially if there is data already in the file (either previous writes that succeeded, or because you are appending).

It is also likely that any further attempts to write to a file that didn't write all n bytes will also fail or write 0, without outside action. The circumstance that springs to mind is exhaustion of free disk space.