c++ - Showing txt file details -


i have .txt file these contents:

good   bad   hi  

i want next button cycle through these words, code word "good". when click next, doesn't show next word. here code 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));     } } 

every time method executed:

void mainwindow::on_next_clicked() {     ui->showen->clear();     ifstream sfile("e:\\en.txt");     getdata(sfile);    sfile.close(); } 

the file opened anew , first bit read getdata. file closed. every time file opened, reading starts on beginning, why see same string every time.

you want open file somewhere else (the window's constructor, perhaps?), read on each button click. require member variable in mainwindow hold open file. don't close until you're done.


Comments

Popular posts from this blog

c++ - No viable overloaded operator for references a map -

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - Cannot secure connection using TLS -