K.Robert K.Robert - 1 year ago 78
C++ Question

Is there any way that I can ignore the character(s) sent by “Ctrl+Z” and read data from istream?

Here is my code.(I asked a totally different question which was unclear and marked duplicated, and due to unfamiliarity, I edit it so it looks the same as this one. Sorry for that.)


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) {
cout << "enter elements for v2" << endl;

//I try to ignore character(s) sent by "Ctrl+Z"
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\0x1A');
cin.ignore(std::numeric_limits<std::streamsize>::max(), EOF);
cin >> i;

while (cin >> i) {

for (auto c : v1) {
cout << c << " ";
cout << endl;

for (auto c : v2) {
cout << c << " ";
cout << endl;


I want to press 1 2 Ctrl+Z to add elements for v1, and press 3 4 Ctrl+Z to add elements for v2, but I get a empty vector v2 eventually. Is there any way I can do this? Why do the approaches I take do not work?

Answer Source

If you are working under windows and your stream is opened in "text mode", the windows run time will apply translations to characters in the stream that apply both to line endings ("\n" translated to "\r\n" for outbound streams) as well as end of file. In the case of end-of-file, the CTRL-Z character is interpreted as an end-of-file mark. This is a hold over from the bad old days of CPM that has somehow persisted long beyond any useful life. The moral to the story: if you want absolute control of how data is written to you file, don't use "text" mode!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download