2017-03-04 82 views
1

好的,所以我使用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.readAll(),之後電話in.atEnd()將返回true。要麼刪除in.readlAll()while循環,爲什麼你需要兩個?

+0

感謝您的回覆。我刪除了in.readAll()行,現在它進入while循環。由於某種原因,循環處於無限循環內部。我在csv中只有91行。但我相信我只需遍歷csv文件的行並根據逗號分割它們。我計劃將每行的內容存儲到結構中。你對我的方法有什麼建議嗎?謝謝! –

+0

@MohamedDoumbouya我假設你取消註釋'line = in.readLine();'循環內?否則你的流永遠不會到達最後。如果你需要分割'QString',那麼'QString :: split()'應該可以幫到你。 – Paul