How to rollback lines from cout?

Viewed 40337

I'm coding a task monitoring, which updates tasks' progress using cout. I'd like to display one task progress per line, therefore I have to rollback several lines of the console.

I insist on "several" because \b does the job for one line, but does not erase \n between lines.

I tried std::cout.seekp(std::cout.tellp() - str.length()); but tellp() returns -1 (failure).

6 Answers

You can use first system(" "); for you can use \e[A (Dev-C++) or \u001B[A (Visual Studio)

#include <iostream>
using namespace std;
int main()
{
    system(" ");
    string Input;
    do
    {
        cout << "[#][\e[s";
        cin >> Input;
        cout << "[\e[u" << Input << "]"<<endl;

    } while (2==2);
    return 0;
}

enter image description here

Hope it helps ;) [It should work on Linux.]

// "\e[0K" Clear line from cursor to the end
cout << "\e[A\r\e[0K"<<what_you_want<<endl;
Related