删除包含单词的行
Delete a line that contain a word
我正在尝试删除包含文本文件中特定单词的行但这不起作用
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");
}
预先感谢您
您仅在线相等时删除线路(即 line != deleteContact
)。如果您想删除仅包含此词的行,则应写出以下内容:
if (strstr(line.c_str(), deleteContact.c_str()) == nullptr) ...
相关文章:
- 是否需要删除包含对象的"pair"?
- 链表删除功能的单指针 // 是可能的
- 如果以下行不包含决策或分支,GDB 无法单步跳过函数
- 动态分配列表 - 创建一个函数,用于删除所有包含偶数值的元素
- 删除包含包含动态对象的 STL 容器的智能指针
- 如何删除包含其他向量的向量?
- 如何使用双黑斜杠从字符串中删除单反斜杠
- QT C++删除带有 *(名称包含)的文件
- 为什么CMake要从include_directories()调用中删除一些包含目录
- 我的 RichEdit 控件是否可以包含可单击的链接?
- 不带初始值设定项的构造函数列表,其中包含带有已删除构造函数的对象
- 删除外部包含代码库中的删除
- 删除包含结构的向量的向量
- 仅在删除包含对象的向量时调用自定义»析构函数«
- 如何从 qmake 中的包含路径中删除项目的根目录
- 我的逻辑有什么问题?尝试计算文件中包含特定字符的单词数
- 在包含相同项目的向量中删除一个项目
- 当我按Enter或在其区域外单击鼠标时,如何从可编辑的Qcombobox中删除光标
- 修改正则表达式以包含反斜杠和单引号(以便反斜杠表现为转义字符)
- 为什么C++允许您移动包含已删除移动操作的对象的类