2017-03-04 31 views
1

OK、QT Creator for C++を使用しています。私はgetExcelFileという名前のCSVファイルを解析する機能を作成しています。他のすべてはうまくいきますが、私のコードは何かの理由でwhileループに入りません。これは私を夢中にしています。いくつかの提案が役に立つでしょう!ありがとう。QT Creator - CSVファイルを解析する

void Widget::getExcelFile(){ 
    //Name of the Qfile object. 
    //filename is the directory of the file once it has been selected in prompt 
    QFile thefile(filename); 
    //If the file isn't successfully open 
    if (thefile.open(QIODevice::ReadOnly | QIODevice::Text)){ 
     qDebug() << "File opened successfully"; 
     //Converts text file to stream 
     QTextStream in(&thefile); 
     fileContent=in.readAll(); 
     QString line; 
     while (!in.atEnd()){ 
      //line = textStream.readLine();//reads line from file 
      //Will not enter this loop for some odd reason. 
      qDebug() << "This text does not print out"; 
     } 
    } 

    qDebug() << "This prints out successfully"; 
    ui->textEdit->setPlainText(fileContent); 
} 

答えて

0

その呼び出しin.atEnd()trueを返します後にあなたは、in.readAll()をしました。 in.readlAll()またはwhileループを削除すると、なぜ両方必要ですか?

+0

お返事ありがとうございます。 in.readAll()行を削除して、whileループに入ります。何らかの理由で、ループは無限ループの内部にあります。私はcsvに91行しか持っていません。しかし、私はちょうどcsvファイルの行を繰り返し、コンマに基づいて分割する必要があると信じています。私は、各行の内容を構造体に格納することを計画しています。あなたは私のためのアプローチの提案がありますか?ありがとう! –

+0

@MohamedDoumbouya私は 'line = in.readLine();'のコメントを外したとします。さもなければあなたの流れは決して終わりに達しません。 'QString'を分割する必要がある場合は、' QString :: split() 'が役に立ちます。 – Paul