我知道这是一个奇怪的问题,但有没有办法从控制台读取先前的输入?
就像是:
The fox is brown // line 1
The duck is yellow // line 2
Here where the control is right now_ // but I want to read line 2
PS:我用的是windows
我知道这是一个奇怪的问题,但有没有办法从控制台读取先前的输入?
就像是:
The fox is brown // line 1
The duck is yellow // line 2
Here where the control is right now_ // but I want to read line 2
PS:我用的是windows
如果通过阅读先前的输入,您的意思是在 C++ 程序中,答案是肯定的。标准输入是一个维护读取缓冲区的流。
快速而肮脏地展开流并读取同一行两次
#include <iostream>
#include <string>
using namespace std;
int main()
{
cout << "Enter a line of text and you will see it echo twice\n";
string mystring;
getline(cin, mystring);
cout << mystring;
cout << "\n";
// reverse the input stream by the lengtht of the read string (+1 for the newline)
for (int i = 0; i <= mystring.length(); i++)
{
cin.unget();
}
string mystring2;
getline(cin, mystring2);
cout << mystring2;
cout << '\n';
}
它可以通过一些假设来完成,但会带来一些您可能会发现过于严格的限制。所以最好找到一种不这样做的方法。
使用ncurses库,您可以完全控制终端并将符号读/写到屏幕上的任何位置。但如果你这样做,你将负责控制它,这包括滚动文本。除非您自己实现,否则也无法向上滚动终端。除此之外,您还需要记住屏幕尺寸并处理它的变化。请注意,您的程序也可以在不支持该模式的终端下启动。
所以,如果可以的话,不要乱用它,并将用户输入存储在程序中,而不是将其存储在屏幕上。
这似乎非常接近。
历史 | 尾-n 2