PDA

View Full Version : Re: Issue with deleting text in file



ramin.lich
21st May 2015, 20:48
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())
{
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();

}

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

ramin.lich
22nd May 2015, 11:21
anybody? please :(

wysota
22nd May 2015, 12:16
How is this question related to Qt? It seems that you are using c++ api for accessing files. How do the files know if they are opened for reading or writing?

ChrisW67
22nd May 2015, 20:49
Ifstream and ofstream have a default open mode of read and write respectively.

Where the files go is anybody's guess when relative paths are involved. If anywhere else in the program plays with the current working directory then much fun could result. Since no error checks are being made this will be hard to see.