I need to make a function that every time I click the button, the program read and act according to a line in the log_p.txt file.
However, if I put the read t process inside the function, every time it will read the first line.
void ai_fight::getfile()
{
std::ifstream t("log_p.txt");
ui->pushButton->setEnabled(false);
getline(t, rule);
print_rule(rule);
if(getline(t, p1hand)) print_p1hand(p1hand);
if(getline(t, p1p)) print_p1p(p1p);
if(getline(t, p2hand)) print_p2hand(p2hand);
if(getline(t, p2p)) print_p2p(p2p);
getline(t, announce);
if(announce=="1 eliminated"||announce=="0 eliminated")
{
getline(t, buf);
getline(t, buf);
getline(t, buf);
getline(t, buf);
getline(t, win);
print_win(win);
ui->pushButton->setEnabled(true);
}
else if(announce=="0 winning"||announce=="1 winning")
{
ui->pushButton->setEnabled(true);
}
else qDebug()<<"----------announcement error"<<endl;
ui->pushButton->setEnabled(true);
}
The simplest way is probably to store the ifstream
as a class member.
void ai_fight::getfile()
{
if (!m_t.is_open())
m_t.open("log_p.txt");
...
You probably want to close the file when you reach EOF so that you can loop round.