PDA

View Full Version : data is not appending to the file



sudheer
3rd April 2008, 11:48
Hello all,
i had a problem to access the file operation, when i tried to write something to the file from my GUI(designer file) then it erasing all the data exist in the file and writing only string which i entered in the LineEdit but i want to be append it to the file.
Here is my code



#include<QtCore>
#include<QtGui>
#include"ui_dialog1.h"
#include <stdio.h>


class synopsys : public QDialog,public Ui::dialog1
{
Q_OBJECT

public:
synopsys()
{
setupUi(this);
connect(pushButton1, SIGNAL(clicked()), this , SLOT(save()));
connect(pushButton2, SIGNAL(clicked()), this , SLOT(close()));

}

public slots:

void save()
{
Qt::CheckState state1,state2,state3,state4;

state1 = checkBox1->checkState();
state2 = checkBox2->checkState();
state3 = checkBox3->checkState();
state4 = checkBox4->checkState();

if(state1 == Qt::Checked)
{
fileName = "/home/window1/synopsys_startup.txt";
QFile file(fileName);
QString str = lineEdit1->text();

const char *ch = str.toLatin1();
file.open(QIODevice::WriteOnly);
file.write(ch, qstrlen(ch));
file.close();

}




if(state2 == Qt::Checked)
{

fileName = "/home/window1/synopsys_startup.txt";
QFile file(fileName);
QString str = lineEdit2->text();

const char *ch = str.toLatin1();
file.open(QIODevice::WriteOnly);
file.write(ch, qstrlen(ch));
file.close();


}


if(state3 == Qt::Checked)
{

fileName = "/home/window1/synopsys_startup.txt";
QFile file(fileName);
QString str = lineEdit3->text();

const char *ch = str.toLatin1();

file.open(QIODevice::WriteOnly);
file.write(ch, qstrlen(ch));
file.close();



}


if(state4 == Qt::Checked)
{

fileName = "/home/window1/synopsys_startup.txt";
QFile file(fileName);
QString str = lineEdit4->text();

const char *ch = str.toLatin1();
file.open(QIODevice::WriteOnly);
file.write(ch, qstrlen(ch));
file.close();



}


}
private:
QString fileName;

};

int main(int argc, char *argv[])
{

QApplication app(argc,argv);

synopsys *startup = new synopsys;
startup->show();

return app.exec();
}

#include "main.moc"


any help or suggestion will be much appreciated

wysota
3rd April 2008, 11:55
You have to open the file in proper mode. "WriteOnly" overwrites the current content of the file. You have to use Append.

sudheer
3rd April 2008, 12:39
thanks i dint observe that