0

我正在使用 Boost Tee_device 类将 Text 输出到cout和 a ofstream。使用这个例子。唯一的区别是我的 TeeDevice 的 typedef 看起来像这样。

typedef io::tee_device<ostream, ofstream> TeeDevice;
typedef io::stream< TeeDevice > TeeStream;

那部分效果很好。

我的问题是在尝试使用流对象编写进度条时出现的。我'\r'用来将课程送回行首。

这是我使用的代码的样子,

//Some code that inits TeeStream* Tee_streamObj;

(*Tee_streamObj) << "Progress:"<< "\n" <<std::flush();

for(int x = 0; x <= 100; x++)
{
   (*Tee_streamObj) << '\r' << (x*100)/100<<"%"<<std::flush();
}

(*Tee_streamObj) << '\r' << "Complete!"<<std::flush();

在 cout 我得到 2 行,第二行不断被覆盖,最后看起来就像这样。

进度:
完成!//<-- 行不断根据需要被覆盖

但是从 ofstream 生成的文件看起来像这样,

进度:
0%
1%
2%

...

99%
100%
完成!

有没有办法覆盖在 boosts Tee_device 类中写入 ofstream 的最后一行?

我正在使用 1.40 版的 boost 库,应该注意我无法控制使用不同的版本。

想法?

提前谢谢。

编辑:我一直在尝试将 ofstream 变成 fstream。有没有办法使用 seekg 或 tellg 来完成这样的事情?

4

0 回答 0