[LIP] std::cin.readsome(...) and std::cin.rdbuf()->in_avail()
"Josy P. Pullockara" <[email protected]>
| Newsgroups | gmane.user-groups.linux.india.programmers |
|---|---|
| Organization | National Aerospace Laboratories |
| Message-ID | <[email protected]> |
Hi,
When it is undesirable to wait for input from a keyboard to std::cin we
use either std::cin.readsome() to peek at the stream. Also
std::cin.in_avail() can be used for similar purposes.
But, how to know in this situation whether std::cin has received <EOF>
as both the above functions do not set the ios_base::eofbit.
For example,
#include <iostream>
void parse(std::istream& is)
{
const int BUF_SIZE = 1024;
char buf[BUF_SIZE + 1];
std::cin.getline(buf, BUF_SIZE);
std::cout << buf << std::endl;
}
int main(void)
{
while (std::cin) {
if (std::cin.rdbuf()->in_avail()) parse(std::cin);
else /* do something */ ;
}
return 0;
}
will NOT quit when <ctrl-D> is pressed. It does quit if followed by a \n
ie., <Enter>. What can be done in main() to quit when <ctrl-D> is
pressed? Should not std::istream::readsome() and in_avail() set the
ios_base::eofbit?
Thank you.
--
Josy P. Pullockara <[email protected]>
National Aerospace Laboratories
-------------------------------------------------------
This SF.net email is sponsored by: SF.net Giveback Program.
Does SourceForge.net help you be more productive? Does it
help you create better code? SHARE THE LOVE, and help us help
YOU! Click Here: http://sourceforge.net/donate/