PDA

View Full Version : Update txt file after clearing line in Qt



mai.osman
20th December 2010, 13:57
I am trying to update the text file after I cleared a line in the file. When I print after the clear the line is actually cleared but when I reopen the txt file the line is still there.
Here is the Code.


void myAccounts::on_removeB_clicked()
{
myFile = new QFile("accountsDB.txt");
if(!myFile->open(QIODevice::Text | QIODevice::ReadWrite))
qDebug()<<"1!!"<<endl;
else
qDebug()<<"3!!"<<endl;
stream= new QTextStream(myFile);
QStringList list;

while(!stream->atEnd())
{
line=stream->readLine();
list = line.split(",");
qDebug()<<ui->tableWidget->currentItem()->text()<<endl;
qDebug()<<list[0]<<endl;
qDebug()<<line<<endl;

if(ui->tableWidget->currentItem()->text() == list[0] || ui->tableWidget->currentItem()->text() == list[1])
{
line.clear();
qDebug()<<line<<endl;
myFile->close();
}
}
ui->tableWidget->removeRow(ui->tableWidget->currentRow());
}

Lykurg
20th December 2010, 14:28
I see no instruction to save the new content to the file.

high_flyer
20th December 2010, 14:52
You will have to read the whole file and hold that data in a temp variable, clear the file content, remove the line from the content, and write back the content with the line cleared.