tags:

views:

224

answers:

1

Hi,

I am doing following in the attached file 1.txt.

a) read a list b) for each item in the list c) run an external process save it in A file d) read A file, save modification in B file e) thatz it

and following error occurs.

ASSERT failure in QList:perator[]: "index out of range", file /opt/qtsdk-2009.02/qt/include/QtCore/qlist.h, line 403

2.txt is one instance of file 1.txt ( no loop ) and it runs successfully.

How is it possible to have the loop run successfully modifying the file. Please suggest with an example. That will help.

PS: you will need file queue_output.txt in tmp folder to run this code.

1.txt

#include <QtCore/QCoreApplication>
#include <QRegExp>
#include <QProcess>
#include <QDebug>
#include <QTemporaryFile>
#include <QDir>


int main(int argc, char *argv[])
{
//      int ret = 0;
        QString projNm = "edisni";
        QCoreApplication app(argc, argv);
        QStringList arguments;

        QString queue;
        QStringList queueList;
        QTextStream out;
// ---------------------------------------------
// till here i get a list of queues in queueList
// ---------------------------------------------

        QFile file("/tmp/queue_output.txt");
        if(file.open(QIODevice::ReadOnly))
        {
            QTextStream in(&file);
            queue = in.readAll();
        }
        file.close();

        queueList = QString(queue).split("\n");

        if (QString(queueList.last()).isEmpty())
            queueList.removeLast();


// ----------------------------------------------
// for each item in queueList run an external command
// store output in _XXXXXX

        qDebug() << "length of queue" << queueList.length();

        for(int i =0; i < queueList.length();i++)
        {
            QProcess *procQueueContent = new QProcess();
            QString lineWithoutSlashAnne_02;

            QTemporaryFile *tFile = new QTemporaryFile(QDir::tempPath()+ "/" + queueList[i] + "_XXXXXX");
            tFile->open();

            QFile pFile("/tmp/queue_content_output.txt");
            pFile.open(QIODevice::WriteOnly);

            procQueueContent->setStandardOutputFile(tFile->fileName());
            procQueueContent->setStandardErrorFile("/tmp/queue_content_error.txt");

            arguments << "-sq" << queueList[i];
            procQueueContent->start("qconf",arguments);

            procQueueContent->waitForReadyRead();
            procQueueContent->waitForFinished();

            tFile->close();
            tFile->open();

            QTextStream out(&pFile);
            QString line;

// ---------------------------------------------
// find word projects in the file, add a word at end
// save output in queue_content_output
// ---------------------------------------------

            while(!tFile->atEnd())
            {

                line = tFile->readLine();
                QStringList lineWithoutSlashAnne_01 = QString(line).split("\n");

                lineWithoutSlashAnne_02 = lineWithoutSlashAnne_01[i];
                if (lineWithoutSlashAnne_02.contains(QRegExp("^projects")))
                {
                    lineWithoutSlashAnne_02.append(" "+projNm);
//  come a line back remove the project line and add this line
                    qDebug() << "project " << lineWithoutSlashAnne_02;

                }
                out << lineWithoutSlashAnne_02 << endl;
            }

            tFile->close();
            pFile.close();

            arguments.clear();
        }
        return app.exec();
}

2.txt

#include <QtCore/QCoreApplication>
#include <QRegExp>
#include <QProcess>
#include <QDebug>
#include <QTemporaryFile>
#include <QDir>
#include <iostream>


int main(int argc, char *argv[])
{
//      int ret = 0;
        QString projNm = "edisni";
        QCoreApplication app(argc, argv);
        QStringList arguments;


// ---------------------------------------------
// till here i get a list of queues in queueList
// ---------------------------------------------

        QString queue;
        QStringList queueList;
        QFile file("/tmp/queue_output.txt");
        if(file.open(QIODevice::ReadOnly))
        {
            QTextStream in(&file);
            queue = in.readAll();
        }
        file.close();

        queueList = QString(queue).split("\n");

        if (QString(queueList.last()).isEmpty())
            queueList.removeLast();

// ---------------------------------------------
// for only first item in queueList[0] run external comm 
// ---------------------------------------------

        QProcess *procQueueContent = new QProcess();
        QString lineWithoutSlashAnne_02;

        QTemporaryFile *tFile = new QTemporaryFile(QDir::tempPath()+ "/" + queueList[0] + "_XXXXXX");
        tFile->open();

        QFile pFile("/tmp/queue_content_output.txt");

        pFile.open(QIODevice::WriteOnly);

        procQueueContent->setStandardOutputFile(tFile->fileName());
        procQueueContent->setStandardErrorFile("/tmp/queue_content_error.txt");
        arguments << "-sq" << queueList[0];

        procQueueContent->start("qconf",arguments);
        procQueueContent->waitForReadyRead();
        procQueueContent->waitForFinished();


        tFile->close();
        tFile->open();

        QTextStream out(&pFile);
        qDebug() << "pFIle " << &pFile;
        QString line;
        while(!tFile->atEnd())                                          // add a condition to restrict traversing thru file only once
        {
            line = tFile->readLine();
            QStringList lineWithoutSlashAnne_01 = QString(line).split("\n");
            lineWithoutSlashAnne_02 = lineWithoutSlashAnne_01[0];
            if (lineWithoutSlashAnne_02.contains(QRegExp("^projects")))
            {

                lineWithoutSlashAnne_02.append(" "+projNm);

                qDebug() << "project " << lineWithoutSlashAnne_02;
//  come a line back remove the project line and add this line
            }
                out << lineWithoutSlashAnne_02 << endl;            
        }

        tFile->close();
        pFile.close();

        arguments.clear();
        return app.exec();
}

queue_output.txt :

all.q
eqws-069.q
grid.q
grid1.q
grid3.q
test.1.q
A: 

You are using the wrong index here ...

//...
QStringList lineWithoutSlashAnne_01 = QString(line).split("\n");  
lineWithoutSlashAnne_02 = lineWithoutSlashAnne_01[i];
//...

I don't know what you want but index i has got nothing to do with the split of the line QString(line).split("\n");.
I guess you want this lineWithoutSlashAnne_02 = lineWithoutSlashAnne_01[0];

Just replace the ugly while loop with:

foreach(QByteArray line, tFile->readAll().split("\n"))
{
    if(line.endsWith(projects))
    {
        line << " " << projNm;
    }
    out << line << endl;
 }


You have a memory leak, procQueueContent is never deleted.

TimW