Computing.Net > Forums > Programming > Reading ctrl-D from cin in C++

Reading ctrl-D from cin in C++

Reply to Message Icon

Original Message
Name: DarkNova
Date: September 16, 2002 at 21:17:04 Pacific
Subject: Reading ctrl-D from cin in C++
OS: Linux
CPU/Ram: Athlon AMD 1.8+
Comment:

I've written a program in C++ and I need to read in individual characters and detect when the user hits ctrl-D. Something like:

while (!cin.eof())
{
char c = cin.get();
if (((char)c == -1) || (cin.eof())
{
cout << "They hit ctrl-D" << endl;
}
}

If the user hits ctrl-D at the beginning of the line, it will be detected by this code just fine. However, if they hit ctrl-D after they've typed in some text, nothing happens, it looks like the iostream is just ignoring it. Then if they hit ctrl-D right afterwards it will be detected by the if statement in the code like above. Debugging shows that there is no record that ctrl-D was even pressed the first time.

It looks like the C++ iostream is doing something strange with how it handles the ctrl-D keypress, and probably other control keypresses too (like I know ctrl-C works the same way). Is there anyway I can tell the iostream to treat ctrl-D or other control characters just as ordinary characters that I can do logic operations on?


Report Offensive Message For Removal








Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Reading ctrl-D from cin in C++

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 3 Days.
Discuss in The Lounge