dimanche 13 août 2017

Cntrl + Z behaviour in terminal

string s;
while(getline(cin,s)){
    cout << "---" << endl
    for(auto c: s) cout << int(c) << endl;   
}
cout << "Exiting";

If my input is Cntrl+Z, then I press enter once and my program exits immediately.

^Z
Exiting

If I enter a character before press Cntrl+Z, then I have to press enter twice, and my program does not exit.

s^Z

---
115
26

I had always interpreted Cntrl+Z as the EOF character. getline would continue until it reaches this character, at which point getline tests false and my program would exit. I'm curious why my program interprets Cntrl+Z as the substitute character 26, depending on if there is a preceding character or not, and why it was necessary for me to press Enter twice in the second example?

Aucun commentaire:

Enregistrer un commentaire