output string overwrite last string on terminal in Linux with c++

心已入冬 提交于 2021-02-08 02:14:18

问题


Say I have a command line program. Is there a way so that when I say

std::cout << stuff

if I don't do a std::cout << '\n' in between another std::cout << stuff, another output of stuff will overwrite the last stuff on the same line (cleaning the line) starting at the leftmost column?

I think ncurses has the ability to do this? If possible, it would be great if I could say std::cout << std::overwrite << stuff

Where std::overwrite is some sort of iomanip.


回答1:


Have you tried carriage returns \r? This should do what you want.




回答2:


Also it is worth to see the escape character documentation: http://en.wikipedia.org/wiki/ANSI_escape_code

You can do much more than setting carret back to the line beggining position!




回答3:


If you just want to overwrite the last stuff printed and other on the same line kept intact, then you can do something like this:

#include <iostream>
#include <string>

std::string OverWrite(int x) {
    std::string s="";
    for(int i=0;i<x;i++){s+="\b \b";}
    return s;}

int main(){   
    std::cout<<"Lot's of ";
    std::cout<<"stuff"<<OverWrite(5)<<"new_stuff";  //5 is the length of "stuff"
    return(0);
}

Output:

Lot's of new_stuff

The OverWrite() function cleans the previous "stuff" and places the cursor at it's start.

If you want the whole line to be cleaned and print new_stuff in that place then just make the argument of OverWrite() big enough like OverWrite(100) or something like that to clean the whole line altogether.

And if you don't want to clean anything, just replace from the start then you can simply do this:

#include<iostream>

#define overwrite "\r"

int main(){ 
    std::cout<<"stuff"<<overwrite<<"new_stuff";
    return(0);
}



回答4:


Have you tried a std::istream::sentry? You could try something like the following, which would "audit" your input.

std::istream& operator>>(std::istream& is, std::string& input) {
    std::istream::sentry s(is);
    if(s) {
        // use a temporary string to append the characters to so that
        // if a `\n` isn't in the input the string is not read
        std::string tmp;
        while(is.good()) {
            char c = is.get();
            if(c == '\n') {
                is.getloc();
                // if a '\n' is found the data is appended to the string
                input += tmp;
                break;
            } else {
                is.getloc();
                tmp += c;
            }
        }
    }
    return(is);
}

The key part being that we append the characters input to the stream to a temporary variable, and if a '\n' isn't read, the data is chucked.

Usage:

int main() {
    std::stringstream bad("this has no return");
    std::string test;
    bad >> test;
    std::cout << test << std::endl; // will not output data
    std::stringstream good("this does have a return\n");
    good >> test;
    std::cout << test << std::endl;

}

This won't be quite as easy as an iomanip, but I hope it helps.



来源:https://stackoverflow.com/questions/29039603/output-string-overwrite-last-string-on-terminal-in-linux-with-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!