Explaining of my purpose:
my program have two section first reading two word from user(second word is meaning of first one) and save this words in two text file second section is showing or deleting words as desire of user.
What is my issue?:
my program works perfectly but after days will something like this happen:
when i try to delete two words from .txt files(i mean 1 word with meaning of it) first word will delete but second word(meaning) will not delete, it sounds like .txt file corrupted only thing that will fix every thing is delete that text file and create new one and thats very annoying that means lose all informations
note: first word and second word are in separate files. second words are still able to show.
what did i try?:
i try to check that is second word text file open or closed. that sounds like it will open or close in right time.
check that if text file is read-only. that wasnt.
i dont know what really happening here !!!!
here its my delete code am i missing something ?
void MainWindow::on_delete_2_clicked()
{
ofstream tempF;
ofstream tempE;
if(!ui->ShowEn->text().isEmpty() | !ui->ShowFa->text().isEmpty())
{
int ret;
msg.setText("Are you sure you want to delete this words?");
ret= msg.exec();
switch(ret){
input=ui->ShowEn->text();
string line;
ifstream DataE;
DataE.open("en.txt");
tempE.open("tempE.txt");
while(getline(DataE,line)){
if(line != input.toStdString()){
tempE<<line<<"\r\n";
}
}
inputF= ui->ShowFa->text();
string lineF;
ifstream DataF;
tempF.open("tempF.txt");
DataF.open("fa.txt");
while(getline(DataF, lineF)){
if( lineF != inputF.toStdString()){
tempF<<lineF<<"\r\n";
}
}
DataE.close();
DataF.close();
tempE.close();
tempF.close();
sfile.close();
if(DataF.is_open()){
cout<<"OOOOOOOOOOy Its Open";
}
remove("en.txt");
remove("fa.txt");
rename("tempE.txt", "en.txt");
rename("tempF.txt", "fa.txt");
break;
}
}
else{
mass.setText("Fields are empty");
mass.exec();
}
restartApp();
}
void MainWindow::on_delete_2_clicked()
{
ofstream tempF;
ofstream tempE;
if(!ui->ShowEn->text().isEmpty() | !ui->ShowFa->text().isEmpty())
{
QMessageBox msg;
int ret;
msg.setText("Are you sure you want to delete this words?");
msg.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
msg.setIcon(QMessageBox::Warning);
ret= msg.exec();
switch(ret){
case QMessageBox::Yes:
QString input;
input=ui->ShowEn->text();
string line;
ifstream DataE;
DataE.open("en.txt");
tempE.open("tempE.txt");
while(getline(DataE,line)){
if(line != input.toStdString()){
tempE<<line<<"\r\n";
}
}
QString inputF;
inputF= ui->ShowFa->text();
string lineF;
ifstream DataF;
tempF.open("tempF.txt");
DataF.open("fa.txt");
while(getline(DataF, lineF)){
if( lineF != inputF.toStdString()){
tempF<<lineF<<"\r\n";
}
}
DataE.close();
DataF.close();
tempE.close();
tempF.close();
sfile.close();
if(DataF.is_open()){
cout<<"OOOOOOOOOOy Its Open";
}
remove("en.txt");
remove("fa.txt");
rename("tempE.txt", "en.txt");
rename("tempF.txt", "fa.txt");
break;
}
}
else{
QMessageBox mass;
mass.setText("Fields are empty");
mass.exec();
}
restartApp();
}
To copy to clipboard, switch view to plain text mode
im on windows 8.1
Added after 22 minutes:
i notice something new right now when i try to delete all information in second .txt file the size of file will be 3 bytes instead of 0 bytes
Bookmarks