从文本文件中删除最近添加的行
Delete recently added lines from a text file
我正在用C++编写一个程序,它将行写入文本文件。在某些情况下,我希望它删除一堆最近添加的行。代码如下所示:
file << "Some line." << endl; // *
// lots of lines might be written to file here
if (condition2)
// delete all the lines written to file since * including line "Some line."
我该怎么做?
看看 ostream 的seekp
和tellp
方法。
我假设你的file
是某种流。
这只是一个伪代码,因为你也没有提供足够的信息
if (condition2)
{
/*
1. Read back the file lines till "Some line" marker
in say, std::vector<std::string>
2. Discard other lines after that.
3. Write the contents of std::vector into the file
*/
}
相关文章:
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- 如何在C++中从两个单独的for循环中添加两个数组
- POCO::PostgreSQL:如何将std::vector支持添加到`Binder::bind`
- 如何仅为一个函数添加延迟
- 如何防止 c++ 在从浮点型转换为双精度型(不适用于 IO)时添加额外的小数?
- 使用std::transform将一个范围的元素添加到另一个范围中
- 如何将更多文件夹添加到c++include路径
- 如何将元素添加到数组的线程安全函数?
- QT通过C++添加映射QML项目
- 如何将点击的信号和插槽添加到qt中的自定义按钮中
- 如何使用重载的相等(==)运算符向测试用例添加描述
- 为什么Mat类的两个对象可以在不重载运算符+的情况下添加
- 如何防止clang格式在流运算符调用之间添加换行符<<
- 只能向C++添加一定数量的字符
- Qt和C++:将QLineEdit添加到QTabWidget中
- 将QIcon添加到QTableView单元格
- 为什么我必须在C++中添加一个赋值符号来声明一个数组
- 为什么除非添加括号,否则构造函数上的模板替换会失败?
- 从文本文件中删除最近添加的行
- 链表只记住最近添加的对象