while( len ) { a = file.write( ptr, len ); if( a < 0 ) break; ptr += a; len -= a;}
file.write( ptr, len );
#include <QtCore>#define BLOCK 10*1024*1024#define TOTAL 1024*1024*1024int main(int argc, char *argv[]){ int i,a; QByteArray ba; ba.resize( BLOCK ); QFile file("out.dat"); file.open( QIODevice::WriteOnly | QIODevice::Truncate ); for( i=0; i<((TOTAL)/(BLOCK)); i++ ) { a = file.write( ba ); if( a != BLOCK ) qDebug() << a; } return 0;}
C++ (Qt)qint64 QFSFileEnginePrivate::nativeWrite(const char *data, qint64 len){ Q_Q(QFSFileEngine); if (fh || fd != -1) { // stdio / stdlib mode. return writeFdFh(data, len); } // Windows native mode. if (fileHandle == INVALID_HANDLE_VALUE) return -1; qint64 bytesToWrite = DWORD(len); // <- lossy // Writing on Windows fails with ERROR_NO_SYSTEM_RESOURCES when // the chunks are too large, so we limit the block size to 32MB. static const DWORD maxBlockSize = 32 * 1024 * 1024; qint64 totalWritten = 0; do { DWORD blockSize = qMin<DWORD>(bytesToWrite, maxBlockSize); DWORD bytesWritten; if (!WriteFile(fileHandle, data + totalWritten, blockSize, &bytesWritten, NULL)) { if (totalWritten == 0) { // Note: Only return error if the first WriteFile failed. q->setError(QFile::WriteError, qt_error_string()); return -1; } break; } if (bytesWritten == 0) break; totalWritten += bytesWritten; bytesToWrite -= bytesWritten; } while (totalWritten < len); return qint64(totalWritten);}
C++ (Qt)qint64 QFSFileEnginePrivate::writeFdFh(const char *data, qint64 len){ Q_Q(QFSFileEngine); qint64 result; qint64 written = 0; do { // Write blocks of 4k to avoid platform limitations (Windows commonly // bails out if you read or write too large blocks at once). qint64 bytesToWrite = qMin<qint64>(4096, len - written); if (fh) { do { // Buffered stdlib mode. result = qint64(fwrite(data + written, 1, size_t(bytesToWrite), fh)); } while (result == 0 && errno == EINTR); if (bytesToWrite > 0 && result == 0) result = -1; } else { do { // Unbuffered stdio mode. result = QT_WRITE(fd, data + written, bytesToWrite); } while (result == -1 && errno == EINTR); } if (result > 0) written += qint64(result); } while (written < len && result > 0); // If we read anything, return that with success. Otherwise, set an error, // and return the last return value. if (result > 0) return written; q->setError(errno == ENOSPC ? QFile::ResourceError : QFile::WriteError, qt_error_string(errno)); return result;}
C++ (Qt)qint64 QFSFileEnginePrivate::nativeWrite(const char *data, qint64 len)...
C++ (Qt)qint64 QFSFileEnginePrivate::writeFdFh(const char *data, qint64 len)...