[SOLVED] Creating Database text file based application
-
Hey, I'm sandy. I'm very newbie on C++ and Qt.
I've homework from computer class to build database text file based application for the mid of semester.
Read, add, delete database application.
My teacher waives any student to use any programming languages.
I choose C++ and Qt for my homework.
This is the field of my database :
code
name
genderall of my data store to text file. separating by "#" on every field and "\n" for every record.
First I learn to show the text file on textBrowser and success (include messagebox if file missing and create file "contact.txt").
This is printscreen of my application :
!http://www.qtcentre.org/attachment.php?attachmentid=10166&d=1395728497(Form)!and this is the textBrowser :
!http://www.qtcentre.org/attachment.php?attachmentid=10167&d=1395728521(textBrowser)!For the next step I've to get value from line edit(code and name) and radio button(for gender) store it to "contact.txt" and if "contact.txt" isn't null to store the value to the next line of the text file.
how to do it ?Sorry if my english so bad, isn't my native.
Thank you. -
For appending data to file you can use
@QString fileName = "contact.txt";
QFile file(fileName);if(!file.open(QFile::Append | QFile::Text)){ qDebug() << "not open"; return 0; } QTextStream out(&file); out << DataString<< "\n"; file.close();@
-
Thank you, solved after go to manual. And before read your reply, but thanks !
Here is my code :@void tampilan::on_simpan_clicked()
{
QString pinData, nameData;
pinData = ui->pinEdit->text();
nameData = ui->nameEdit->text();
QFile kontak("kontak.txt");
if(!kontak.open(QIODevice::ReadWrite)){
QMessageBox::information(0,"info",kontak.errorString());
}
QTextStream update(&kontak);
QString line;
do{
line = update.readLine();
}while(!line.isNull());
update <<"#"<< pinData;
update <<"#"<< nameData;
if(ui->radioMale->isChecked()==true){
update <<"#M";
}else if(ui->radioFemale->isChecked()==true){
update <<"#F";
}
update <<"\n";
kontak.close();
if(!kontak.open(QIODevice::ReadOnly)){
QMessageBox::information(0,"info",kontak.errorString());
}
ui->pinEdit->clear();
ui->nameEdit->clear();
ui->radioMale->setChecked(true);
QTextStream in(&kontak);
ui->textBrowser->setText(in.readAll());
}@