我正在尝试从文本文件中删除包含特定单词的行 但它不起作用
void deleteline()
{
string line, deletecontact;
cout << "Plase enter the contact (name or number) to delete:";
cin >> deletecontact;
ifstream file;
ofstream outfile;
file.open("data.txt");
outfile.open("newM.txt");
while (getline(file, line)) {
if (line != deletecontact) {
outfile << line << endl;
}
}
outfile.close();
file.close();
remove("movieList.txt");
rename("newM.txt", "data.txt");
}
提前谢谢
答案 0 :(得分:3)
只有当行相等时才删除行(即line != deleteContact
)。如果你想删除那些如你所提到的只包含这个单词的行,你应该写下如下内容:
if (strstr(line.c_str(), deleteContact.c_str()) == nullptr) ...