-1

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

我该怎么做?

4

2 回答 2

0

看看ostream的seekptellp方法。

我假设你file是某种fstream。

于 2013-10-19T10:04:26.620 回答
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

  */
}
于 2013-10-19T10:10:10.897 回答