[Qt-interest] Possible bug in QTextStream when used with QByteArray

Malyushytsky, Alex alex at wai.com
Wed Jul 7 00:40:24 CEST 2010


I have a problem which looks like a bug in QTextStream.

I need to compress some text file which has pairs of numbers except 3 top lines and write it to the file.
To do it I read data from the file into QByteArray using QTextStream, on the 14066 line value read is not equal to value written.
At this point when value is read an extra character which belongs to the next value is read as a part of current value.

// values written into QByteArray
// val =  0.275119
// val =  0.056078  ! this value is CORRECT
// val =  4.28628   ! this value is CORRECT

// values read from the QByteArray
// val =  0.275119
// val =  0.0560784 !!!!!!!!!!! expected 0.056078
// val =  0.28628  !!!!!!!!!!! expected  4.28628

Trying to localize problem allowed to come to conclusion that problem happens either:
 - when value is written to QByteArray using QTextStream out ( &_array, QIODevice::WriteOnly );
 - or when value is read from the same array using QTextStream in ( &_array, QIODevice::ReadOnly );

Any restriction on size of QByteArray I am missing?

QT 4.6.0 MSVC 2005, Windows XP 64 bit.


Regards,
   Alex


///////////////////////////////////////////////////////
// reading data from file into

                                QTextStream in( &file );
                                // read number of fields
                                in >> numFields;
                                if( numFields <1 || numFields>MaxFields || in.status() != QTextStream::Ok )
                                {
                                        label = UserNotifier::tr("Corrupted  or unknown fornat file:' %1'").arg( fileTxt );
                                }
                                else
                                {
                                        QByteArray _array;
                                        _array.reserve( 1024*1024*24 );
                                        QTextStream out ( &_array, QIODevice::WriteOnly );

                                        ok = true;
                                        // write number of fields
                                        out << numFields;

                                        QString name;
                                        for( int i=0; i< numFields; i++ )
                                        {
                                                name.resize(0);
                                                while( name.isEmpty() && in.status() == QTextStream::Ok  )
                                                {
                                                        name = in.readLine();
                                                }
                                                if( name.isEmpty() || in.status() != QTextStream::Ok )
                                                {
                                                        ok=false;
                                                        break;
                                                }
                                                else
                                                {
                                                        out << name <<"\n"; // write field name
                                                }
                                        }
                                        label = UserNotifier::tr("reading field information from the file:'%1' to buffer").arg( fileTxt );
                                        UserNotifier::glNotifyActionStarted( label );

                                        int counter = 0;
                                        double val;
                                        while ( ok && !in.atEnd() )
                                        {
                                                in >> val;

                                                int index = 14066-1;
                                                if( counter >= ( index - 3)*2  && counter <= ( ( index - 3)*2 + 2)  )
                                                {
                                                        qDebug() << " counter =" << counter << "  index = " << index << " val = " << val;

// counter = 28124   index =  14065  val =  0.275119
// counter = 28125   index =  14065  val =  0.056078  ! this value is CORRECT
// counter = 28126   index =  14065  val =  4.28628   ! this value is CORRECT
                                                }

                                                if( in.status() != QTextStream::Ok  )
                                                {
                                                        Q_ASSERT( in.status() != QTextStream::ReadCorruptData  );
                                                        break;
                                                }
                                                else
                                                {
                                                        out << val;
                                                        counter++;
                                                }
                                        }

// _array currently has a CORRECT data read from file

#ifdef DEBUG_CORRECT_ARRAY_WRITTEN
// Reading from the _array, checking that data is correct, right to another file
                                                {
                                                        QString fileNameTmp = fileTxt+ ".createdArray2";
                                                        QFile fileTmp( fileNameTmp );
                                                        if ( fileTmp.open( QIODevice::WriteOnly | QIODevice::Text ) )
                                                        {
                                                                QTextStream tmpOut( &fileTmp );

                                                                QTextStream in ( &_array, QIODevice::ReadOnly );
                                                                in >> numFields;

                                                                tmpOut << numFields << "\n";;

                                                                QString name;

                                                                for( int i=0; i< numFields; i++ )
                                                                {
                                                                        name.resize(0);
                                                                        while( name.isEmpty() && in.status() == QTextStream::Ok  )
                                                                        {
                                                                                in >> name;
                                                                                tmpOut << name << "\n";;
                                                                        }
                                                                }

                                                                for( int nline=0; nline< counter/2; ++nline )
                                                                {
                                                                        for( int i=0; i< numFields; ++i )
                                                                        {
                                                                                in >> val; // any check before we assign the value?
                                                                                int index = 14066-3-1;
                                                                                if( nline >= index && nline <= index+6  )
                                                                                {
                                                                                        qDebug() << " counter =" << counter << "  index = " << nline << " val = " << val;
// Output
// counter = 1464832   index =  14062  val =  0.275119
// counter = 1464832   index =  14062  val =  0.0560784 !!!!!!!!!!! expected 0.056078
// counter = 1464832   index =  14063  val =  0.28628  !!!!!!!!!!! expected  4.28628
                                                                                }

                                                                                tmpOut << val;
                                                                                tmpOut.flush();

                                                                                if ( i == numFields-1 )
                                                                                        tmpOut << "\n";
                                                                                else
                                                                                        tmpOut << " ";

                                                                        }
                                                                }
                                                        }
                                                }
#endif





















counter = 28124   index =  14065  val =  0.275119
counter = 28125   index =  14065  val =  0.056078
counter = 28126   index =  14065  val =  4.28628

counter = 1464832   index =  14062  val =  0.275119
 counter = 1464832   index =  14062  val =  0.0560784
 counter = 1464832   index =  14063  val =  0.28628

Alex Malyushytsky
Research Engineer - Weidlinger Associates Inc.
office: 650 230 0210,
direct: 650 230 0349
web: http://www.wai.com


---------------------------------------------------------------------------------------------------
Weidlinger Associates, Inc. made the following annotations.

“This message and any attachments are solely for the intended recipient and may contain confidential or privileged information. If you are not the intended recipient, any disclosure, copying, use, or distribution of the information included in this message and any attachments is prohibited. If you have received this communication in error, please notify us by reply e-mail and immediately and permanently delete this message and any attachments. Thank you.”

“Please consider our environment before printing this email.”




More information about the Qt-interest-old mailing list