Results 1 to 4 of 4

Thread: Re: Issue with deleting text in file

  1. #1
    Join Date
    Aug 2014
    Posts
    28
    Thanks
    15
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Issue with deleting text in file

    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 ?
    Qt Code:
    1. void MainWindow::on_delete_2_clicked()
    2. {
    3. ofstream tempF;
    4. ofstream tempE;
    5. if(!ui->ShowEn->text().isEmpty() | !ui->ShowFa->text().isEmpty())
    6. {
    7. int ret;
    8. msg.setText("Are you sure you want to delete this words?");
    9. msg.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
    10. msg.setIcon(QMessageBox::Warning);
    11. ret= msg.exec();
    12. switch(ret){
    13. case QMessageBox::Yes:
    14. QString input;
    15. input=ui->ShowEn->text();
    16. string line;
    17. ifstream DataE;
    18.  
    19. DataE.open("en.txt");
    20. tempE.open("tempE.txt");
    21. while(getline(DataE,line)){
    22. if(line != input.toStdString()){
    23. tempE<<line<<"\r\n";
    24. }
    25. }
    26.  
    27. QString inputF;
    28. inputF= ui->ShowFa->text();
    29. string lineF;
    30. ifstream DataF;
    31. tempF.open("tempF.txt");
    32. DataF.open("fa.txt");
    33. while(getline(DataF, lineF)){
    34. if( lineF != inputF.toStdString()){
    35. tempF<<lineF<<"\r\n";
    36. }
    37. }
    38.  
    39.  
    40. DataE.close();
    41. DataF.close();
    42. tempE.close();
    43. tempF.close();
    44. sfile.close();
    45.  
    46. if(DataF.is_open()){
    47. cout<<"OOOOOOOOOOy Its Open";
    48. }
    49.  
    50. remove("en.txt");
    51. remove("fa.txt");
    52.  
    53. rename("tempE.txt", "en.txt");
    54. rename("tempF.txt", "fa.txt");
    55.  
    56.  
    57.  
    58. break;
    59. }
    60.  
    61. }
    62. else{
    63. mass.setText("Fields are empty");
    64. mass.exec();
    65. }
    66. restartApp();
    67.  
    68. }
    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
    Last edited by ramin.lich; 21st May 2015 at 20:56.

  2. #2
    Join Date
    Aug 2014
    Posts
    28
    Thanks
    15
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Issue with deleting text in file

    anybody? please

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Issue with deleting text in file

    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?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Issue with deleting text in file

    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.

Similar Threads

  1. Replies: 5
    Last Post: 11th April 2015, 07:42
  2. Replies: 3
    Last Post: 8th June 2011, 06:36
  3. Deleting a file
    By chandan in forum Newbie
    Replies: 5
    Last Post: 12th August 2010, 14:30
  4. Replies: 1
    Last Post: 3rd September 2008, 14:16
  5. Deleting a part of a file
    By Sivert in forum General Programming
    Replies: 1
    Last Post: 25th March 2008, 23:43

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.