unbuffered input with cin

I need to gather terminal input (console, pterm or
whatever…) using cin (an iostream) WITHOUT
buffering.

Something similar to the getch() function found
in some dos compilers.

Maybe there are some way to disable buffering
in the basic_streambuf present inside cin?

Thanks in advance.


John [ipv7.net]

Do you mean that you want to grab characters from the terminal as soon
as the key is pressed rather than waiting for them to hit enter? If so,
you need to set raw input mode on the stream. Now I don’t know if this
works for cin/cout but here’s how you do it with C on a file descriptor.
I would expect that cin/cout/cerr are reading/writing from 0/1/2 so this
might work.

int raw(int fd)
{
struct termios termios_p;

if( tcgetattr( fd, &termios_p ) )
return( -1 );

termios_p.c_cc[VMIN] = 1;
termios_p.c_cc[VTIME] = 0;
termios_p.c_lflag &= ~( ECHO|ICANON|ECHOE|ECHOK|ECHONL );
termios_p.c_oflag &= ~( OPOST );

return( tcsetattr( fd, TCSADRAIN, &termios_p ) );
}

int unraw(int fd)
{
struct termios termios_p;

if( tcgetattr( fd, &termios_p ) )
return( -1 );

termios_p.c_lflag |= ( ECHO|ICANON|ISIG|ECHOE|ECHOK|ECHONL );
termios_p.c_oflag |= ( OPOST );

return( tcsetattr( fd, TCSADRAIN, &termios_p ) );
}

Hope that works…There’s probably an officially sanctioned C++ way to do
it but I don’t know how myself.

cheers,

Kris

john@broadcast.ipv7.net wrote:

I need to gather terminal input (console, pterm or
whatever…) using cin (an iostream) WITHOUT
buffering.

Something similar to the getch() function found
in some dos compilers.

Maybe there are some way to disable buffering
in the basic_streambuf present inside cin?

Thanks in advance.


John [ipv7.net]


Kris Warkentin
kewarken@qnx.com
(613)591-0836 x9368
“Computer science is no more about computers than astronomy is about telescopes”
–E.W.Dijkstra

Kris Eric Warkentin <kewarken@qnx.com> wrote:

Do you mean that you want to grab characters from the terminal as soon
as the key is pressed rather than waiting for them to hit enter? If so,
you need to set raw input mode on the stream. Now I don’t know if this
works for cin/cout but here’s how you do it with C on a file descriptor.
I would expect that cin/cout/cerr are reading/writing from 0/1/2 so this
might work.

int raw(int fd)
{
struct termios termios_p;

if( tcgetattr( fd, &termios_p ) )
return( -1 );

termios_p.c_cc[VMIN] = 1;
termios_p.c_cc[VTIME] = 0;
termios_p.c_lflag &= ~( ECHO|ICANON|ECHOE|ECHOK|ECHONL );
termios_p.c_oflag &= ~( OPOST );

return( tcsetattr( fd, TCSADRAIN, &termios_p ) );
}

int unraw(int fd)
{
struct termios termios_p;

if( tcgetattr( fd, &termios_p ) )
return( -1 );

termios_p.c_lflag |= ( ECHO|ICANON|ISIG|ECHOE|ECHOK|ECHONL );
termios_p.c_oflag |= ( OPOST );

return( tcsetattr( fd, TCSADRAIN, &termios_p ) );
}

Hope that works…There’s probably an officially sanctioned C++ way to do
it but I don’t know how myself.

That’s exactly what I’ve nedeed. An istream effectively doesn’t do
line-buffering when using istream::get(), but since the stream itself
was buffered, also the getchar function never returned me characters
“as they go”.

Thanks a lot

cheers,

Kris

john@broadcast.ipv7.net > wrote:
I need to gather terminal input (console, pterm or
whatever…) using cin (an iostream) WITHOUT
buffering.

Something similar to the getch() function found
in some dos compilers.

Maybe there are some way to disable buffering
in the basic_streambuf present inside cin?

Thanks in advance.


John [ipv7.net]


Kris Warkentin
kewarken@qnx.com
(613)591-0836 x9368
“Computer science is no more about computers than astronomy is about telescopes”
–E.W.Dijkstra


John [ipv7.net]

In article <9rk33q$e8o$1@nntp.qnx.com>,
Kris Eric Warkentin <kewarken@qnx.com> wrote:

Hope that works…There’s probably an officially sanctioned C++ way to do
it but I don’t know how myself.

There’s not, and so you need to end up doing something else,
like you’ve shown.

Greg Comeau export ETA: December See our Oct 31st special
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware’s Libraries… Have you tried it?

I’ve often thought that it would be nice if all of the standard C stuff was
wrapped in a more ‘C++ like’ interface. There’s always so much mixing and
matching and you wind up with goofy things like sync_with_stdio to let C
and C++ calls be used in the same program. Oh well.

cheers,

Kris

Greg Comeau <comeau@panix.com> wrote:

In article <9rk33q$e8o$> 1@nntp.qnx.com> >,
Kris Eric Warkentin <> kewarken@qnx.com> > wrote:
Hope that works…There’s probably an officially sanctioned C++ way to do
it but I don’t know how myself.

There’s not, and so you need to end up doing something else,
like you’ve shown.

Greg Comeau export ETA: December See our Oct 31st special
Comeau C/C++ ONLINE ==> > http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware’s Libraries… Have you tried it?


Kris Warkentin
kewarken@qnx.com
(613)591-0836 x9368
“Computer science is no more about computers than astronomy is about telescopes”
–E.W.Dijkstra

In article <9rp2ln$j5r$1@nntp.qnx.com>,
Kris Eric Warkentin <kewarken@qnx.com> wrote:

Greg Comeau <> comeau@panix.com> > wrote:
In article <9rk33q$e8o$> 1@nntp.qnx.com> >,
Kris Eric Warkentin <> kewarken@qnx.com> > wrote:
Hope that works…There’s probably an officially sanctioned C++ way to do
it but I don’t know how myself.

There’s not, and so you need to end up doing something else,
like you’ve shown.

I’ve often thought that it would be nice if all of the standard C stuff was
wrapped in a more ‘C++ like’ interface. There’s always so much mixing and
matching and you wind up with goofy things like sync_with_stdio to let C
and C++ calls be used in the same program. Oh well.

I’ve often thought that too. OTOH, there is no officially
sanctioned C way to do what was being asked either, so it
wouldn’t help in this case.

Greg Comeau export ETA: December See our Oct 31st special
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware’s Libraries… Have you tried it?

Greg Comeau <comeau@panix.com> wrote:

In article <9rk33q$e8o$> 1@nntp.qnx.com> >,
Kris Eric Warkentin <> kewarken@qnx.com> > wrote:
Hope that works…There’s probably an officially sanctioned C++ way to do
it but I don’t know how myself.

Uhm, I’ve the need to extract the fd from an fstream now, maybe somebody
knows how to to this with the dinkum library?
It seems that the fd is private!

There’s not, and so you need to end up doing something else,
like you’ve shown.

Greg Comeau export ETA: December See our Oct 31st special
Comeau C/C++ ONLINE ==> > http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware’s Libraries… Have you tried it?


John [ipv7.net]

I don’t have documentation in front of me but I seem to recall that there is
a .fd() method. Try that. If that’s not it I know it is something similar.
If I see documentation contrary I’ll post it here.


Bill Caroselli – 1(530) 510-7292
Q-TPS Consulting
QTPS@EarthLink.net


<john@broadcast.ipv7.net> wrote in message news:9rslgh$37o$1@inn.qnx.com

Greg Comeau <> comeau@panix.com> > wrote:
In article <9rk33q$e8o$> 1@nntp.qnx.com> >,
Kris Eric Warkentin <> kewarken@qnx.com> > wrote:
Hope that works…There’s probably an officially sanctioned C++ way to
do
it but I don’t know how myself.

Uhm, I’ve the need to extract the fd from an fstream now, maybe somebody
knows how to to this with the dinkum library?
It seems that the fd is private!

There’s not, and so you need to end up doing something else,
like you’ve shown.

Greg Comeau export ETA: December See our Oct 31st special
Comeau C/C++ ONLINE ==> > http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware’s Libraries… Have you tried it?


John [ipv7.net]

Bill Caroselli <qtps@earthlink.net> wrote:

I don’t have documentation in front of me but I seem to recall that there is
a .fd() method. Try that. If that’s not it I know it is something similar.
If I see documentation contrary I’ll post it here.

Dinkum seems to miss this method!
I’ve read all the fstream header and I’ve found no way to get it! :frowning:(((

Actually after some search a filebuf::fd() method seems to exist
under many c++ libraries, but not into this one.

No one know how to extract this &%@# descriptor?


John [ipv7.net]


Bill Caroselli – 1(530) 510-7292
Q-TPS Consulting
QTPS@EarthLink.net



john@broadcast.ipv7.net> > wrote in message news:9rslgh$37o$> 1@inn.qnx.com> …
Greg Comeau <> comeau@panix.com> > wrote:
In article <9rk33q$e8o$> 1@nntp.qnx.com> >,
Kris Eric Warkentin <> kewarken@qnx.com> > wrote:
Hope that works…There’s probably an officially sanctioned C++ way to
do
it but I don’t know how myself.

Uhm, I’ve the need to extract the fd from an fstream now, maybe somebody
knows how to to this with the dinkum library?
It seems that the fd is private!

There’s not, and so you need to end up doing something else,
like you’ve shown.

Greg Comeau export ETA: December See our Oct 31st special
Comeau C/C++ ONLINE ==> > http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware’s Libraries… Have you tried it?


John [ipv7.net]