-3

Im writing about searching a room number deleting a line which includes wanted room number from txt in c++ but it deleting everything, what's wrong with it? And its all of false code, can you suggest me a new algorithm?

                    int sil;
                    cout << "Silmek istediğiniz oda numarası : ";
                    cin >> sil;
                    oda.Sil(sil);
                    fstream veriler,gecici;
                    veriler.open("Veriler.txt", ios::out || ios::in || ios::app);
                    gecici.open("Gecici.txt", ios::out || ios::in || ios::app);
                    while (!veriler.eof())
                    {
                        veriler >> oda.oda_Numara;
                        veriler >> oda.musteri_Ad;
                        veriler >> oda.musteri_Soyad;
                        veriler >> oda.oda_Ucret;
                        veriler >> oda.musteri_Kimlik;
                        veriler >> oda.musteri_Numara;
                        if (oda.oda_Numara == sil)
                            continue;
                        else
                        {
                            gecici << setw(20) << oda.oda_Numara
                                << setw(20) << oda.musteri_Ad
                                << setw(20) << oda.musteri_Soyad
                                << setw(20) << oda.oda_Ucret
                                << setw(20) << oda.musteri_Kimlik
                                << setw(20) << oda.musteri_Numara
                                << endl;
                        }
                }
                    remove("Veriler.txt");
                    rename("Gecici.txt", "Veriler.txt");
                    gecici.close();
  • 1. Check if you have actually opened the files 2. Read this [while eof is bad](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – Ed Heal Nov 28 '17 at 18:35
  • 3. Perhaps use a debugger – Ed Heal Nov 28 '17 at 18:37
  • 3
    Possible duplicate of [Deleting specific line from file](https://stackoverflow.com/questions/26576714/deleting-specific-line-from-file) – Yorki Bonilla Nov 28 '17 at 18:40
  • 2
    1. Copy all wanted text lines to new file. 2. Skip over unwanted lines. 3. Repeat until end of original file. 4. Rename original file. 5. Rename new file. – Thomas Matthews Nov 28 '17 at 18:45

1 Answers1

0

I edited my answer and I think this is more what you are looking for:

string buf;
fstream file, toGo;
file.open("somefile.txt");
toGo.open("temp-file.txt", ios::out);

while(getline(file,buf)) 
{
    if(buf.find("0") != -1) toGo<<buf;
}
file.close(); 
rename("temp-file.txt", "somefile.txt");

Hope this is better!

Jake Freeman
  • 1,700
  • 1
  • 8
  • 15
  • That would be a waste of memory for large files (not to mention you are ignoring whitespace, you should be using `std::getline()` instead of `operator>>`). Better to just open a second file and write lines to it while reading from the source file, then close the files and replace the source file with the new file. Just as Thomas Matthews described in his comment. – Remy Lebeau Nov 28 '17 at 19:33
  • buddy but i want search and delete like, delete line in room number i want to delete, is it useful for sentence in a txt? – A. Göktuğ Yalçın Nov 28 '17 at 20:46
  • @GöktuğYalçın my revised answer allows you to plug in the number or text and as long as it is not in the line it won't delete that line. – Jake Freeman Nov 29 '17 at 03:36