如何在命令窗口的一个位置编写变量并在同一位置更新其值
How can I write a variable in one place of command window and update its value at the same place?
例如输出为 a=20 我想将数字"20"更改为另一个数字,并将结果写入第一个输出的同一位置,而不是在新行中(不需要最早数量的"a",只是最后一个结果很重要)我尽量避免这样的事情:
输出:
a=20
a=21
a=70
.
.
.
你试过这个吗:
printf("ra=%d",a);
// r=carriage return, returns the cursor to the beginning of current line
从形式上讲,通用解决方案需要类似 ncurses
.实际上,如果您正在寻找的只是有这样的行:
a = xxx
其中xxx
是一个不断发展的值,您可以输出行没有'n'
(或std::flush
而不是std::endl
);要更新,只需输出足够的b
字符即可返回到数。 像这样:
std::cout << "label = 000" << std::flush;
while ( ... ) {
// ...
if ( timeToUpdate ) {
std::cout << "bbb" << std::setw(3) << number << std::flush;
}
}
这假设固定宽度格式(在我的示例中,没有值大于 999)。 对于可变宽度,您可以先格式化为std::ostringstream,为了确定退格数下次你必须输出。 我会使用特殊的计数器类型为此:
class DisplayedCounter
{
int myBackslashCount;
int myCurrentValue;
public:
DisplayedCounter()
: myBackslashCount(0)
, myCurrentValue(0)
{
}
// Functions to evolve the current value...
// Could be no more than an operator=( int )
friend std::ostream& operator<<(
std::ostream& dest,
DisplayedCounter const& source )
{
dest << std::string( myBackslashCount, 'b' );
std::ostringstream tmp;
tmp << myCurrentValue;
myBackslashCount = tmp.str().size();
dest << tmp.str() << std::flush();
return dest;
}
};
上一次我不得不这样做(当恐龙在地球上漫游,牛仔布很酷的时候),我们使用了诅咒。
您可以存储所需的所有输出,并在每次更改值时重新绘制整个控制台窗口。在Linux上不知道,但在Windows上,您可以使用以下内容清除控制台:
system("cls");
相关文章:
- 非常量变量只读位置的赋值
- 你好。。。id_public变量不应该给出结果为 81 和 86 吗?为什么它为两个派生类占用不同的内存位置?
- 在 elf 文件中查找全局变量的位置
- C++强制变量到一个固定的内存位置
- 当使用Lua作为嵌入式语言(比如c++)时,有什么简单/方便的方法可以找到变量在Lua中的定义位置吗
- 函数,返回变量c++占用的内存位置的大小
- 在 Visual Studio 2010(及更高版本)中定义包含或链接路径变量的位置
- 为什么动态分配的两个变量的内存位置不是连续的?
- ClangTool 如何获取变量声明中模板参数的位置?
- 如何跟踪通过外部代码修改类变量的所有位置
- 适用于会员变量的新位置
- C++每个变量的存储位置是否正确?
- 可视C 编译器将相同的存储位置提供给两个变量
- 双变量的精度仅为十点后的2个位置
- *的位置有什么区别:*变量与变量*
- 引用变量何时合适,为什么?你能解释一下实际的语法和位置吗?
- Windows SDK 8:关于安装位置,文件夹命名法和Windowssdkdir_old Env变量的混乱
- 获取通过环境变量访问的文件的位置
- C++变量定义中的易失性类型量词位置
- C++健全性检查失败:几个变量/内存位置被更改为垃圾,即使我从未访问过它们