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?
so there is no function in Qt that write exactly n bytes or return error
well, that exactly whatQIODevice::write
do with error condition ask != n
– Alexey Andronov-1
, aboutk
<=n
, there is no mention that on error it returnsk >= 0
andk != n
, so may be in some cases it is normal – user1244932k == -1 || k != n
wherek
is return value ofwrite
. There's no need of such a simple function in a library when you can write a wrapper yourself :) – Alexey Andronov