我在附件1.txt。
中进行了以下操作a)阅读清单 b)列表中的每个项目 c)运行外部进程将其保存在A文件中 d)读取A文件,保存B文件中的修改 e)那就是
发生以下错误。
在QList中的ASSERT失败:perator []:“索引超出范围”,文件/opt/qtsdk-2009.02/qt/include/QtCore/qlist.h,第403行
2.txt是文件1.txt(无循环)的一个实例,它运行成功。
如何使循环运行成功修改文件。 请举个例子来建议。这会有所帮助。
PS:你需要tmp文件夹中的文件queue_output.txt来运行这段代码。
的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
答案 0 :(得分:0)
你在这里使用了错误的索引......
//...
QStringList lineWithoutSlashAnne_01 = QString(line).split("\n");
lineWithoutSlashAnne_02 = lineWithoutSlashAnne_01[i];
//...
我不知道你想要什么,但是索引i
与行QString(line).split("\n");
的分割无关。
我想你想要这个lineWithoutSlashAnne_02 = lineWithoutSlashAnne_01[0];
< BR />
只需用以下内容替换丑陋的while循环:
foreach(QByteArray line, tFile->readAll().split("\n"))
{
if(line.endsWith(projects))
{
line << " " << projNm;
}
out << line << endl;
}
您有内存泄漏,procQueueContent
永远不会被删除。