Can't read file more than n bytes



  • Hey guys,

    I'm trying to parse a json file, however for some unknown reason it fails to read it completely. I can read 29276 bytes, then it suddenly stops. It doesn't matter if I try to save the content as QByteArray or QString. The qDebug() line is simply not executed, all code after works...

    http://pastebin.com/raw/eQ9v0AyT

    QFile dataFile("data.json");
    if(dataFile.open(QIODevice::ReadOnly))
    {
    	QByteArray ba = dataFile.readAll();
    	qDebug() << "File Content: " << ba;
    	dataFile.close();
    }
    

    Any help is appreciated. Thanks in advance!



  • Hi,
    maybe qDebug() << ba; just doesn't show it all. Can you try qDebug() << ba.count();
    -Michael.



  • @m.sue dataFile.count() or dataFile.size() etc do work fine. What doesn't work is outputting the ba itself or stuff like ba.trimmed()



  • Yes, but my point is about what it is that does not work: The reading itself or the output of the read-in bytearray via qDebug.
    Please add the line:

    qDebug() << ba.count();
    

    -Michael.



  • @m.sue sorry, typo. didn't mean dataFile.count(). "qDebug() << ba.count();" does work fine and give proper output.

    When reading more than my limit, for example with "QByteArray ba = dataFile.read(29277)", ba.count() also does work. So yes, the reading works.

    Adding the following returns "true". So the doc is also empty when using the ByteArray.

    QJsonDocument doc = QJsonDocument::fromJson(ba);
                    qDebug() << doc.isEmpty();
    


  • That may be restriction in the buffer length of the (place where you view the) qDebug output.
    Maybe just view it in two parts will show you the results:

    qDebug() << ba.right(25000);
    qDebug() << ba.mid(25000);    //+/- 1
    


  • @m.sue said in Can't read file more than n bytes:

    That may be restriction in the buffer length of the (place where you view the) qDebug output.
    Maybe just view it in two parts will show you the results:

    qDebug() << ba.right(25000);
    qDebug() << ba.mid(25000);    //+/- 1
    

    This actually works for output. I've used:

                    qDebug() << "AAA";
                    qDebug() << ba.left(dataFile.size()/2);
                    qDebug() << "BBB";
                    qDebug() << ba.right(dataFile.size()/2);
                    qDebug() << "CCC";
    

    Anyway, the buffer also seems to be there when doing:

    QJsonDocument doc = QJsonDocument::fromJson(ba);

    If I split the file, the json is invalid and can't be put with fromJson anymore. This seems to be a very weird limitation?!



  • Have you tried the following steps to get a valid document:

    1. Allocate a char-array of the size you need.
    2. Copy the file-content using QTextStream to this array.
    3. Get a valid QJsonDocument by using QJsonDocument::fromRawData

  • Moderators

    @metaDom I think it is a limitation of qDebug: it just stops at some point to not to print too much. You can try to print with std::cout << ba;


  • Lifetime Qt Champion

    Hi,

    Do you have any special character in your .json file ?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.