Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Can't read file more than n bytes
Forum Updated to NodeBB v4.3 + New Features

Can't read file more than n bytes

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 5 Posters 1.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • metaDomM Offline
    metaDomM Offline
    metaDom
    wrote on last edited by metaDom
    #1

    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!

    1 Reply Last reply
    0
    • m.sueM Offline
      m.sueM Offline
      m.sue
      wrote on last edited by
      #2

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

      metaDomM 1 Reply Last reply
      2
      • m.sueM m.sue

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

        metaDomM Offline
        metaDomM Offline
        metaDom
        wrote on last edited by
        #3

        @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()

        1 Reply Last reply
        0
        • m.sueM Offline
          m.sueM Offline
          m.sue
          wrote on last edited by
          #4

          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.

          metaDomM 1 Reply Last reply
          0
          • m.sueM m.sue

            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.

            metaDomM Offline
            metaDomM Offline
            metaDom
            wrote on last edited by metaDom
            #5

            @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();
            
            1 Reply Last reply
            0
            • m.sueM Offline
              m.sueM Offline
              m.sue
              wrote on last edited by
              #6

              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
              
              metaDomM 1 Reply Last reply
              0
              • m.sueM m.sue

                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
                
                metaDomM Offline
                metaDomM Offline
                metaDom
                wrote on last edited by
                #7

                @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?!

                R jsulmJ 2 Replies Last reply
                0
                • metaDomM metaDom

                  @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?!

                  R Offline
                  R Offline
                  raspe88
                  wrote on last edited by
                  #8

                  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
                  1 Reply Last reply
                  0
                  • metaDomM metaDom

                    @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?!

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @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;

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    1
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      Hi,

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

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved