0

I'm using the Boost Tee_device class to output Text to both cout and a ofstream. Using this example. Only difference is that my typedef for TeeDevice looks like this.

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

That part works great.

My problem comes when trying to write a progress bar using the stream object. I'm using '\r' to send the courser back to the beginning of the line.

Here's what the code I'm using looks like,

//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();

In cout I get 2 lines, the second line keeps getting overwritten and finally looking just like this.

Progress:
Complete! //<-- line keeps getting overwritten as desired

but the file generated from the ofstream looks like this,

Progress:
0%
1%
2%

...

99%
100%
Complete!

Is there a way to overwrite the last line written to an ofstream in boosts Tee_device class?

I'm using version 1.40 of the boost library and should note that I don't have any control over using a different version.

Ideas?

Thanks ahead of time.

Edit: I've been playing around with making the ofstream into a fstream. Is there a way to accomplish something like this using seekg or tellg?

Community
  • 1
  • 1
Dan
  • 2,625
  • 7
  • 39
  • 52
  • 1
    Why are you trying to output a progress bar (a device for indicating progress to a human reader) to a text file? The overall problem is that "carriage return" is a concept for terminals (or printers), and doesn't apply to files in the same way. – Dave S Aug 06 '12 at 18:01
  • @DaveS Can you suggest something that might work for files in a similar way? Also I wouldn't be against splitting the process bar functionality if need be to get it to look consistent. – Dan Aug 06 '12 at 18:20
  • @DaveS I'm printing it to a text file to keep a record of what the users were displayed. This allows the user to read the output without being forced to redirecting it to a file and then view it from there. The reasoning is so it doesn't disturb their usual work flow if there are no errors. But if there are errors they have a way of communicating it to a coder even if they didn't save the output in the command prompt. – Dan Aug 06 '12 at 20:21

0 Answers0