2010-12-14 5 views
2

다른 파일에서 동시에 여러 파일 (임의 번호) 파일을 열고 qlist에 텍스트 스트림을 저장하려고합니다.QT : QList에 QTextStream 저장

QList<QTextStream> files; 
QList<QString> fnames; 
fnames.append("file1.txt"); 
fnames.append("file2.txt"); 
// ..and so on with random iterations 

// collect qtextsrams into qlist 
foreach (QString file, fnames) { 
     QFile f(file); 
     f.open(QIODevice::ReadOnly); 
     QTextStream textStream(&f); 
     files2.append(&textStream); 
} 

// use qtextstreams in a loop 
QList<QTextStream>::iterator i; 
for (i = files.begin(); i != files.end(); ++i) { 
     qDebug() << i->readLine(); 
} 

그래서 오류가 있습니다 :

/make debug 
make -f Makefile.Debug 
make[1]: Entering directory `/home/pixx/Workspace/collocs' 
g++ -c -pipe -g -Wall -W -D_REENTRANT -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++ -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4 -Idebug -o debug/main.o main.cpp 
main.cpp: In function ‘int main(int, char**)’: 
main.cpp:128: error: no matching function for call to ‘QList<QTextStream>::append(QTextStream*)’ 
/usr/include/qt4/QtCore/qlist.h:493: note: candidates are: void QList<T>::append(const T&) [with T = QTextStream] 
/usr/include/qt4/QtCore/qlist.h:819: note:     void QList<T>::append(const QList<T>&) [with T = QTextStream] 
main.cpp:117: warning: unused variable ‘cc’ 
In file included from /usr/include/qt4/QtCore/QList:1, 
       from main.cpp:1: 
/usr/include/qt4/QtCore/qtextstream.h: In member function ‘void QList<T>::node_copy(QList<T>::Node*, QList<T>::Node*, QList<T>::Node*) [with T = QTextStream]’: 
/usr/include/qt4/QtCore/qlist.h:695: instantiated from ‘void QList<T>::detach_helper(int) [with T = QTextStream]’ 
/usr/include/qt4/QtCore/qlist.h:709: instantiated from ‘void QList<T>::detach_helper() [with T = QTextStream]’ 
/usr/include/qt4/QtCore/qlist.h:126: instantiated from ‘void QList<T>::detach() [with T = QTextStream]’ 
/usr/include/qt4/QtCore/qlist.h:254: instantiated from ‘QList<T>::iterator QList<T>::begin() [with T = QTextStream]’ 
main.cpp:133: instantiated from here 
/usr/include/qt4/QtCore/qtextstream.h:258: error: ‘QTextStream::QTextStream(const QTextStream&)’ is private 
/usr/include/qt4/QtCore/qlist.h:386: error: within this context 
/usr/include/qt4/QtCore/qtextstream.h:258: error: ‘QTextStream::QTextStream(const QTextStream&)’ is private 
/usr/include/qt4/QtCore/qlist.h:399: error: within this context 
make[1]: Leaving directory `/home/pixx/Workspace/collocs' 
make[1]: *** [debug/main.o] Error 1 
make: *** [debug] Error 2 

내가 고쳐야 할까? 나는 그것이 아주 간단한 질문 인 것을 이해한다, 그러나 google를위한 적당한 질문을 찾아 낼 수 없다 : (

답변

4

첫 번째 오류, ''QList :: append (QTextStream *) '에 대한 호출에 일치하는 함수가 없습니다. 당신이이 줄 & 연산자를 사용하여 :.

귀하의 목록이 QTextStream 객체로 만들어 질 예정이다
files2.append(&textStream); 

하지 포인터

을 객체를 QTextStream합니다하지만 진짜 문제는 더 깊은 거짓말에 객체를 넣으려면. 목록에서 객체가 복사 생성자를 가져야합니다. QTextStream에는 diff가없는 방법이 명확하지 않으므로 아무 것도 없습니다. 동일한 텍스트 스트림의 여러 복사본이 함께 작동해야합니다. "QList"에서와 같이 텍스트 스트림에 대한 포인터 목록을 만드는 것이 좋습니다. 더 이상 필요하지 않을 때 물론,이 경우, 자신의 파괴를 처리하는 것을 잊지 마세요 :

foreach (QTextStream *cur, files) delete cur; 

당신이 코드의 다른 부분 사이의 목록을 통과 그것 등의 여러 복사본을해야 할 경우 , 스마트 포인터 (QSharedPointer)가 필요할 수도 있지만, 텍스트 스트림을 처리해야하는 작업은 거의 생각할 필요가 없습니다.

+0

아마도 내 초기 문제에 대한 더 간단한 해결책이 있을까요? 난 파일의 무작위 번호를 열고 그 후 다시 열지 않고 그들 중 일부에서 단일 라인을 읽을 필요가있다. – pixx

+0

병합 순서가 지정된 파일을 하나로 통합하는 알고리즘이 필요합니다 : // N 개의 결과 파일 열기 // 각 파일의 행을 읽습니다. // - 두 파일을 모두 읽을 때까지 : // - 단어입니다. 동일? // - 예 -> 출력 파일에 단어와 합계를 씁니다. // 두 파일에서 다음 행을 읽습니다. // - 아니요 - 알파벳으로 가장 낮은 단어 만 출력합니다. // 읽기 이 단어의 결과 파일의 다음 줄 – pixx

2

솔루션을 찾았습니다. 감사합니다. Septagram for idea! QList 파일 2; // 병합 할 파일 목록

QList<QTextStream> files; 
QList<QString> fnames; 
fnames.append("file1.txt"); 
fnames.append("file2.txt"); 
// ..and so on with random iterations 
QList<QFile *> files2; // file list to merge 
QList<QTextStream *> files3; 
foreach (QString file, files) { 
    files2.append(new QFile(file)); // create file obj 
    files2.last()->open(QIODevice::ReadOnly); // open file 
    files3.append(new QTextStream(files2.last())); // create textstream 
} 

QList< QTextStream * >::iterator i3; 
for (i3 = files3.begin(); i3 != files3.end(); ++i3) { 
     qDebug() << (*i3)->readLine(); 
} 
관련 문제