PDA

View Full Version : Showing txt file details one by one



ramin.lich
22nd April 2015, 21:43
i have txt file with this details :
good
bad
hi
now i want to show this words one by one after i click on Next button but with this code i only get "Good" text i mean when i click on next button doesnt show me other words. in overall my goal is when i click on next show me "good" word after i click it again show me "bad" after i click again show me "hi" here is my code with Qt 5.4


void MainWindow::on_Next_clicked()
{
ui->ShowEn->clear();
ifstream Sfile("E:\\en.txt");
getData(Sfile);
Sfile.close();
}
void MainWindow::getData(std::ifstream& myfile){

if(!myfile.eof())
{
std::string str;
getline(myfile, str);
ui->ShowEn->setText(QString::fromStdString(str));
}
}

Santosh Reddy
23rd April 2015, 02:07
You are re-opening the file on every click of Next button, instead open the file only once and call getLine() on every Next click.