【问题标题】:output string overwrite last string on terminal in Linux with c++输出字符串用c ++覆盖Linux终端上的最后一个字符串
【发布时间】:2015-03-13 18:38:13
【问题描述】:

假设我有一个命令行程序。有没有办法让我说的时候

std::cout << stuff

如果我在另一个std::cout &lt;&lt; stuff 之间不做std::cout &lt;&lt; '\n',另一个输出的东西会覆盖从最左边一列开始的同一行(清理行)上的最后一个东西?

我觉得有这个能力吗?如果可能的话,如果我能说std::cout &lt;&lt; std::overwrite &lt;&lt; stuff 那就太好了

std::overwrite 是某种 iomanip。

【问题讨论】:

    标签: ncurses c++ linux ncurses iomanip


    【解决方案1】:

    你试过回车\r吗?这应该可以满足您的需求。

    【讨论】:

    • 不要忘记在\r 后面加上std::flush,否则你可能会写很多因为iostream 正在缓存它而很长时间都不会出现的“东西”。 (例如std::cout &lt;&lt; stuff &lt;&lt; '\r' &lt;&lt; std::flush;
    【解决方案2】:

    还有值得一看的转义字符文档:http://en.wikipedia.org/wiki/ANSI_escape_code

    您可以做的不仅仅是将 carret 设置回行首位置!

    【讨论】:

      【解决方案3】:

      如果您只想覆盖最后打印的内容并且同一行上的其他内容保持不变,那么您可以执行以下操作:

      #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);
      }
      

      输出:

      Lot's of new_stuff
      

      OverWrite() 函数清除之前的“东西”并将光标放在它的开始处。

      如果您想清理整行并在其中打印 new_stuff 然后将OverWrite() 的论点设置得足够大,例如 OverWrite(100) 或类似的东西来清理整条生产线 完全一致。

      如果您不想清理任何东西,只需从头开始更换,然后您就可以这样做:

      #include<iostream>
      
      #define overwrite "\r"
      
      int main(){ 
          std::cout<<"stuff"<<overwrite<<"new_stuff";
          return(0);
      }
      

      【讨论】:

      • 介意解释一下您的解决方案吗?
      • 点赞!完美的解释,优雅的解决方案。
      【解决方案4】:

      你试过std::istream::sentry吗?您可以尝试以下类似的方法,它会“审核”您的输入。

      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);
      }
      

      关键部分是我们将输入到流中的字符附加到一个临时变量中,如果没有读取 '\n',则数据被丢弃。

      用法:

      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;
      

      }

      这不会像iomanip 那样简单,但我希望它会有所帮助。

      【讨论】:

        猜你喜欢
        • 2017-01-31
        • 2017-01-26
        • 2015-01-04
        • 2013-08-22
        • 1970-01-01
        • 2022-07-06
        • 1970-01-01
        • 1970-01-01
        • 2015-05-01
        相关资源
        最近更新 更多