有什么方法可以忽略“Ctrl+Z”发送的字符并从istream读取数据吗?
Is there any way that I can ignore the character(s) sent by “Ctrl+Z” and read data from istream?
这是我的代码。(我问了一个完全不同的问题,该问题不清楚并标记为重复,由于不熟悉,我对其进行了编辑,使其看起来与此代码相同。抱歉。)
#include<iostream>
#include<vector>
using std::cin;
using std::cout;
using std::endl;
using std::vector;
int main()
{
int i;
vector<int> v1;
vector<int> v2;
cout << "enter elements for v1" << endl;
while (cin >> i) {
v1.push_back(i);
}
cout << "enter elements for v2" << endl;
//I try to ignore character(s) sent by "Ctrl+Z"
cin.clear();
/*cin.sync();*/
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cin.ignore(std::numeric_limits<std::streamsize>::max(), '[=10=]x1A');
cin.ignore(std::numeric_limits<std::streamsize>::max(), EOF);
cin >> i;
while (cin >> i) {
v2.push_back(i);
}
for (auto c : v1) {
cout << c << " ";
}
cout << endl;
for (auto c : v2) {
cout << c << " ";
}
cout << endl;
}
我想按 1 2 Ctrl+Z 为 v1 添加元素,按 3 4 Ctrl+Z 为 v2 添加元素,但我最终得到一个空矢量 v2。有什么办法可以做到这一点?为什么我采取的方法不起作用?
我想我误会了 ignore 的用法,http://www.cplusplus.com/reference/istream/istream/ignore/,只要删除所有包含 "ignore" 的语句,我就得到了预期的答案。
这是我的代码。(我问了一个完全不同的问题,该问题不清楚并标记为重复,由于不熟悉,我对其进行了编辑,使其看起来与此代码相同。抱歉。)
#include<iostream>
#include<vector>
using std::cin;
using std::cout;
using std::endl;
using std::vector;
int main()
{
int i;
vector<int> v1;
vector<int> v2;
cout << "enter elements for v1" << endl;
while (cin >> i) {
v1.push_back(i);
}
cout << "enter elements for v2" << endl;
//I try to ignore character(s) sent by "Ctrl+Z"
cin.clear();
/*cin.sync();*/
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cin.ignore(std::numeric_limits<std::streamsize>::max(), '[=10=]x1A');
cin.ignore(std::numeric_limits<std::streamsize>::max(), EOF);
cin >> i;
while (cin >> i) {
v2.push_back(i);
}
for (auto c : v1) {
cout << c << " ";
}
cout << endl;
for (auto c : v2) {
cout << c << " ";
}
cout << endl;
}
我想按 1 2 Ctrl+Z 为 v1 添加元素,按 3 4 Ctrl+Z 为 v2 添加元素,但我最终得到一个空矢量 v2。有什么办法可以做到这一点?为什么我采取的方法不起作用?
我想我误会了 ignore 的用法,http://www.cplusplus.com/reference/istream/istream/ignore/,只要删除所有包含 "ignore" 的语句,我就得到了预期的答案。