[英]redirecting std::cin stream back and forth
i want to read the first part of my input from *.txt and get the rest manually from user. 我想从* .txt中读取输入的第一部分,并从用户手动获取其余部分。 for instance:
例如:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string a[100];
for (int i=0;i<100;i++)
cin >> a[i];
for (int i=0;i<100;i++)
cout << a[i] << endl;
}
if i run the above code like ./a.out < input.txt and input.txt only consists of 10 words for example, how can i redirect the input stream back to console so that i get the rest from user ? 如果我运行上面的代码,如./a.out <input.txt和input.txt只包含10个单词,例如,如何将输入流重定向回控制台,以便我从用户那里得到其余的?
You said: 你说:
i want to read the first part of my input from *.txt and get the rest manually from user.
我想从* .txt中读取输入的第一部分,并从用户手动获取其余部分。
If this is the only requirement, I would suggest a change of strategy. 如果这是唯一的要求,我建议改变策略。
cin
. cin
读取其余数据。 For example: 例如:
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char** argv)
{
string a[100];
int count = 0;
if ( argc > 1 )
{
ifstream infile(argv[1]);
while (count < 100)
{
infile >> a[count];
if ( infile )
{
++count;
}
else
{
break;
}
}
}
for (int i=count;i<100;i++)
cin >> a[i];
for (int i=0;i<100;i++)
cout << a[i] << endl;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.