PDA

View Full Version : Appending data, if file exists.



rookee
10th December 2015, 14:54
Hello All,

Please take a look at the following code. I have the following 2 questions.



FILE *filename1;

void MainWindow::update1(void)
{

//something
//something

filename1 = fopen(filename.toLocal8Bit(),"a");

//something
}

void MainWindow::on_checkBox_clicked()
{

if(ui->checkBox->isChecked())
{

filename = QFileDialog::getSaveFileName(...)
/*I'm creating a file when the checkBox is checked. When the checkBox is unchecked and checked back again, If the user selects existing file I want to append data
instead of creating a new file. How do I do it?*/

if(!ui->checkBox->isChecked()){

//I want to append empty lines to the file a created above and close it. How can I do that?



Please help. Thanks in advance.

Vikram.Saralaya
10th December 2015, 15:11
if(ui->checkBox->isChecked())
{
filename = QFileDialog::getSaveFileName(...);
file = new QFile(filename);
file->open(QIODevice::Append | QIODevice::Text))
{
qDebug() << "File opened in append mode" << filename;
}
}

if(!ui->checkBox->isChecked())
{
QTextStream fout(file);
fout << '\n' << '\n' << '\n';
file->close();
}