如何从txt中删除想要的行

How to delete wanted line from txt

我正在写关于搜索房间号的文章,在 C++ 中从 txt 中删除了包含所需房间号的行,但它删除了所有内容,这有什么问题?而且都是假代码,你能给我推荐一个新算法吗?

                    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();

我编辑了我的答案,我认为这更符合您的要求:

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");

希望这会更好!