从文本文件中删除最近添加的行

时间:2013-10-19 09:58:45

标签: c++ file text-files line

我正在用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."

我该怎么做?

2 个答案:

答案 0 :(得分:0)

查看ostream的seekptellp方法。

我认为你的file是某种fstream。

答案 1 :(得分:0)

这只是一个伪代码,因为你也没有提供足够的信息

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

  */
}