writeCharacters() of QXmlStreamWriter crashed when writting the content of a big file to QBuffer

39 Views Asked by At

The following code use writeCharacters with small buffer size to writing the content of big file, but it seems it only works when writing the content to a xml file, but writeCharacters() will crash when writting the content of a big file to QBuffer, any solution? Thanks.

env: qt opensource 4.8.7; Visual Studio 2010; Windows 10;

big file: https://1drv.ms/u/s!Ap_EAuwC9QkXijbujBQcQk4Hat_O?e=KemgUY

#include <QtCore/QCoreApplication>
#include <QFile>
#include <QXmlStreamWriter>
#include <QBuffer>

int main(int argc, char *argv[])
{
   
    QCoreApplication a(argc, argv);

    QByteArray mContentBuffer;
    QByteArray arr;
    int pos, filesize;

    QFile file("C:\\Work\\bigfile.xar"); //Szie: 300Mb

    if(!file.open(QFile::ReadOnly))
    {
        return -1;
    }

    mContentBuffer = file.readAll();
    file.close();

    //QFile profile("C:\\Work\\profile.xml");

    //if(!profile.open(QFile::WriteOnly|QFile::Truncate))
    //{
    //    return -1;
    //}
    QBuffer buffer;
    buffer.open(QBuffer::ReadWrite);

    QXmlStreamWriter stream(&buffer);    

    stream.setAutoFormatting(true);
    stream.writeStartDocument();
    stream.writeStartElement("Profile");

    //stream.writeTextElement("Content", mContentBuffer.toBase64());
    stream.writeStartElement("Content");
    pos = 0; 
    filesize = mContentBuffer.size();         
    while(pos<filesize){
       arr = mContentBuffer.mid(pos, 2000000);
       stream.writeCharacters(arr.toBase64());
       pos+=arr.size();
    }
    stream.writeEndElement();
    stream.writeEndElement(); // Profile
    stream.writeEndDocument();

    return 0;
}

Here is the goal for writing a big file into a QBuffer.

bool profileModified()
{
    bool result = true;

    QFile profile("C:\\Work\\profile.xml");
    if(profile.exists())
    {
        QBuffer buffer;
        buffer.open(QBuffer::ReadWrite);
        QXmlStreamWriter stream(&buffer);
        
        exportProfile(stream);

        profile.open(QFile::ReadOnly);
        QByteArray profileArr = profile.readAll();
    
        buffer.seek(0);
      
        QByteArray bufferArr = buffer.buffer();
         
        result = (array_compare(profileArr, bufferArr) != 0);
        
        profile.close();
    }

    return result;
}
0

There are 0 best solutions below