2009-12-20 8 views
0

첨부 파일 1.txt에서 다음을 수행하고 있습니다.루프에서 qprocess를 실행할 때 qlist 오류가 발생합니다.

은 a), 파일 d) 파일을 판독에 저장하는 외부 프로세스를 실행

그것을 thatz B 파일 e)에 변형을 저장)리스트 C 각 항목리스트 B)를 읽어

다음 오류가 발생합니다. perator은 [] : QList에

ASSERT 실패 "지표 범위 외"/opt/qtsdk-2009.02/qt/include/QtCore/qlist.h 파일 라인 (403)

2.txt는 하나 개의 인스턴스 1.txt (루프 없음) 파일의 성공적으로 실행됩니다.

어떻게 루프를 실행하여 파일을 성공적으로 수정할 수 있습니까? 예를 들어 제안 해주십시오. 도움이 될 것입니다.

추 신 :이 코드를 실행하려면 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 
+1

디버거를 사용하여 실행했는지, 색인에서 벗어난 경우 즉, 코드의 어느 부분이 잘못 되었습니까? – e8johan

+0

C++에서 함수와 클래스를 만들어 코드를 구조화 할 수 있다는 것을 알고 계십니까? – TimW

답변

0

...

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

원하는 것을 알지 못하지만 인덱스 i은 라인 QString(line).split("\n");의 분할과 관련이 없습니다.
난 당신과 함께 루프 동안 그냥 추악한 교체 lineWithoutSlashAnne_02 = lineWithoutSlashAnne_01[0];

이 원하는 것 같아요 :

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

당신은 메모리 누수가가, procQueueContent가 삭제되지 않습니다.

관련 문제