Re: [LIP] std::cin.readsome(...) and std::cin.rdbuf()->in_avail()

Bhaskar Dutta <[email protected]>
Newsgroups gmane.user-groups.linux.india.programmers
Organization Bhaskie Inc.
Message-ID <[email protected]>
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1



  >>> On Tuesday 02 December 2003 08:33 pm, JP <Josy P. Pullockara> said:

    JP> When it is undesirable to wait for input from a keyboard to std::cin
 we JP> use either std::cin.readsome() to peek at the stream. Also
    JP> std::cin.in_avail() can be used for similar purposes.
    JP>
    JP> But, how to know in this situation whether std::cin has received
 <EOF> JP> as both the above functions do not set the ios_base::eofbit. JP>
    JP> For example,
    JP>
[snip]
    JP> int main(void)
    JP> {
    JP> 	while (std::cin) {
    JP> 		if (std::cin.rdbuf()->in_avail()) parse(std::cin);
    JP> 		else /* do something */ ;
    JP> 	}
    JP> 	return 0;
    JP> }
    JP> will NOT quit when <ctrl-D> is pressed. It does quit if followed by a
 \n JP> ie., <Enter>. What can be done in main() to quit when <ctrl-D> is JP>
 pressed? Should not std::istream::readsome() and in_avail() set the JP>
 ios_base::eofbit?
    JP>
    JP>

Hi,
 In UNIX, pressing ctrl-D at the start of a line acts as end-of-file. Pressing
it somewhere else within a line in your program doesn't work. (check it out!)
You have to test the stream for EOF if you want to exit with ctrl-d.

The easiest and most common way is to test a stream within a conditional, as
EOF sets the fail flag.

For example, instead of using just :

	while (std::cin){ ....}

declare a string (or whatever data you wanna force from user), eg str, and do

	while (std::cin >> str){.....}

As soon as you press ctrl-d, this conditional fails and your program exits.
Now you have the input stored in a string and you can manipulate it as you
want. The fail flag is set when any stream operation fails, like attempting
to read a name fails due to end of file, or something other than the expected
data type appears in the stream

Your code suggests that you want to manipulate the stream directly. So you wud
probably have to find a workaround if you want to use cin::rdbuf->in_avail()
etc. or else change the code as cin is no longer required. In fact it gets
simpler and shorter this way.

Regards,
Bhaskar.

- --

- ---------------------------
Bhaskar Dutta <[email protected]> <[email protected]>
     GPG .eq. AA56 1EB5 D7E8 DD9C 298E  8F4D 375F D416 01D5 671C
- ---------------------------

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (GNU/Linux)

iD8DBQE/zRmeN1/UFgHVZxwRAiAcAJ9j3txT3rfhxiu4x2Hr9Cah0A/VpwCgjSw8
Trps+Kwsq7OmbQbASbfdybc=
=zKMo
-----END PGP SIGNATURE-----




-------------------------------------------------------
This SF.net email is sponsored by OSDN's Audience Survey.
Help shape OSDN's sites and tell us what you think. Take this
five minute survey and you could win a $250 Gift Certificate.
http://www.wrgsurveys.com/2003/osdntech03.php?site=8
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.