gpt4 book ai didi

c++ - 删除从 cout 输出到控制台的最后一行?

转载 作者:太空狗 更新时间:2023-10-29 20:37:18 25 4
gpt4 key购买 nike

制作一个带有百分比进度条的程序,虽然它确实很垃圾。它创建了 100,000 个数字,并以 1% 2% 3% 的感觉显示它实际的进展情况,唯一的问题是,它让控制台充斥着这些数字,每增加​​一个百分比大约有一千个数字.

这可能无关紧要,但这是我正在使用的代码

int testing() {
cout << "Open file\n";
fstream outFile("text.txt", ios::out);
int number = 100000;
for (int i = 1; i != number + 1; i++) {
outFile << i << endl;
cout << (i / 1000) << endl;
//cout << clearLastLine();
}
outFile.close();
cout << "File Closed\n";
return 0;
}

注释的 cout 是我想要使用的(据我所知它不存在)。每次 cout << i/1000 << endl;触发器,我希望它删除刚刚放入控制台窗口的那个数字,以便可以用下一个数字替换它。这可能吗?如果是,我很想知道 - 谢谢。 -- 下图显示了为什么我要清除输出的最后一行。 lots of 99% then 100% to close

减少垃圾邮件的新代码,但我仍然得到 100 行我不想要的控制台,但我仍然希望显示最新的数字,只是其他数字不显示。

int testing() {
cout << "Open file\n";
fstream outFile("text.txt", ios::out);
int number = 100000;
int percentifier = number * .01; // added this so I can change number to be anything I want
int temp2 = 0;
for (int i = 1; i != number + 1; i++) {
outFile << i << endl;
int temp = i / percentifier;
if (temp != temp2) {
//cout << clearLastLine();
cout << (temp) << "%" << endl;
}
temp2 = i / percentifier;
}
outFile.close();
cout << "File Closed\n";
return 0;
}

最佳答案

好吧,由于我进一步细化了我的问题,一个带有我正在寻找的答案的问题出现在可能答案的列表中。瞧,我找到了我想要的东西。

使用'\r'修饰符,而不是endl;在我的 cout << (temp) << "%" << endl;写入后立即删除当前行,并用下一个数字覆盖它。

关于c++ - 删除从 cout 输出到控制台的最后一行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35258108/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com