Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Newbie Turn off Echo in C

Reply
Thread Tools

Newbie Turn off Echo in C

 
 
Chris
Guest
Posts: n/a
 
      10-13-2003
Hey all I know how to turn off echoing in a shell but I want to know
how I can do that in a C program when using getchar or scanf . Thanks
for your help.....
 
Reply With Quote
 
 
 
 
Joona I Palaste
Guest
Posts: n/a
 
      10-13-2003
Chris <(E-Mail Removed)> scribbled the following:
> Hey all I know how to turn off echoing in a shell but I want to know
> how I can do that in a C program when using getchar or scanf . Thanks
> for your help.....


Short answer:
You can't.

Long answer:
When you write text from your keyboard, it doesn't go to C's stdin. It
goes to your console, which does two things:
(1) write it to your screen, and
(2) send it to C's stdin.
As by the time C ever sees your text it has already been written to your
screen, C has a snowball's chance of erasing it from your screen.
Therefore you have to prevent your console from writing it there in the
first place. How you do this is very much OS-specific and can't be done
in standard C.

--
/-- Joona Palaste ((E-Mail Removed)) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"I wish someone we knew would die so we could leave them flowers."
- A 6-year-old girl, upon seeing flowers in a cemetery
 
Reply With Quote
 
 
 
 
Chris
Guest
Posts: n/a
 
      10-14-2003
Joona I Palaste <(E-Mail Removed)> wrote in message news:<bmejpt$2dg$(E-Mail Removed)>...
> Chris <(E-Mail Removed)> scribbled the following:
> > Hey all I know how to turn off echoing in a shell but I want to know
> > how I can do that in a C program when using getchar or scanf . Thanks
> > for your help.....

>
> Short answer:
> You can't.
>
> Long answer:
> When you write text from your keyboard, it doesn't go to C's stdin. It
> goes to your console, which does two things:
> (1) write it to your screen, and
> (2) send it to C's stdin.
> As by the time C ever sees your text it has already been written to your
> screen, C has a snowball's chance of erasing it from your screen.
> Therefore you have to prevent your console from writing it there in the
> first place. How you do this is very much OS-specific and can't be done
> in standard C.



Well I would totally have to disagree. After searching around I found
snippet of code that can stop text from going to the console. Take a
look below. This is on a Linux Box. Turning off echoing in a root
shell with stty is easy but this worked using C.

static struct termios stored_settings;
void echo_off(void)
{
struct termios new_settings;
tcgetattr(0,&stored_settings);
new_settings = stored_settings;
new_settings.c_lflag &= (~ECHO);
tcsetattr(0,TCSANOW,&new_settings);
return;
}
 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      10-14-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (Chris) wrote:

> Joona I Palaste <(E-Mail Removed)> wrote in message news:<bmejpt$2dg$(E-Mail Removed)>...
> > How you do this is very much OS-specific and can't be done
> > in standard C.

^^^^^^^^^^

> Well I would totally have to disagree. After searching around I found
> snippet of code that can stop text from going to the console. Take a
> look below. This is on a Linux Box. Turning off echoing in a root
> shell with stty is easy but this worked using C.
>
> static struct termios stored_settings;
> void echo_off(void)
> {
> struct termios new_settings;
> tcgetattr(0,&stored_settings);
> new_settings = stored_settings;
> new_settings.c_lflag &= (~ECHO);
> tcsetattr(0,TCSANOW,&new_settings);
> return;
> }


And how is this Standard C? This is not ISO, and therefore off-topic here -
but probably on-topic on a Linux programming group. Which is exactly what
Joona wrote.

Richard
 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      10-14-2003
On 14 Oct 2003 08:23:11 -0700, in comp.lang.c , (E-Mail Removed)
(Chris) wrote:

>>
>> Short answer:
>> You can't.
>>

> Well I would totally have to disagree. After searching around I found
>snippet of code that can stop text from going to the console. Take a
>look below. This is on a Linux Box. Turning off echoing in a root
>shell with stty is easy but this worked using C.


I hate to point this out to you, but this code is not C.

>static struct termios stored_settings;


implementation specific extension

>void echo_off(void)
>{
>struct termios new_settings;


implementation specific extension

>tcgetattr(0,&stored_settings);


implementation specific extension

>new_settings = stored_settings;
>new_settings.c_lflag &= (~ECHO);
>tcsetattr(0,TCSANOW,&new_settings);


implementation specific extension

>return;


Style point: you can't return from a void function.
>}


ok, this line is C.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>


----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      10-14-2003
In <(E-Mail Removed) > (E-Mail Removed) (Chris) writes:

> Well I would totally have to disagree. After searching around I found
>snippet of code that can stop text from going to the console. Take a
>look below. This is on a Linux Box. Turning off echoing in a root
>shell with stty is easy but this worked using C.


Well, you can always use stty in a C program (think system()) to avoid
getting your hands dirty.

>static struct termios stored_settings;
>void echo_off(void)
>{
>struct termios new_settings;
>tcgetattr(0,&stored_settings);
>new_settings = stored_settings;
>new_settings.c_lflag &= (~ECHO);
>tcsetattr(0,TCSANOW,&new_settings);
>return;
>}


Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: (E-Mail Removed)
 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      10-14-2003
Mark McIntyre <(E-Mail Removed)> wrote:

> On 14 Oct 2003 08:23:11 -0700, in comp.lang.c , (E-Mail Removed)
> (Chris) wrote:
>
> >void echo_off(void)


> >return;

>
> Style point: you can't return from a void function.


Wrong. You can't return _a value_ from a void function. You can
certainly return from it. After all, how else would you return from it
prematurely? C is not Pascal, after all.

Richard
 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      10-14-2003
On Tue, 14 Oct 2003 15:59:39 GMT, in comp.lang.c ,
(E-Mail Removed) (Richard Bos) wrote:

>Mark McIntyre <(E-Mail Removed)> wrote:
>
>> On 14 Oct 2003 08:23:11 -0700, in comp.lang.c , (E-Mail Removed)
>> (Chris) wrote:
>>
>> >void echo_off(void)

>
>> >return;

>>
>> Style point: you can't return from a void function.

>
>Wrong. You can't return _a value_ from a void function. You can
>certainly return from it.


Thats what I meant. The return is useless in this example.

>After all, how else would you return from it
>prematurely?


By using the One True Control Style. To misquote highlander: "there
can /be/ only one return point".

>C is not Pascal, after all.


and here's me having 1-based arrays as recently as y'day. Shoot me.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>


----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      10-15-2003
Mark McIntyre <(E-Mail Removed)> wrote:

> On Tue, 14 Oct 2003 15:59:39 GMT, in comp.lang.c ,
> (E-Mail Removed) (Richard Bos) wrote:
>
> >Mark McIntyre <(E-Mail Removed)> wrote:
> >
> >> On 14 Oct 2003 08:23:11 -0700, in comp.lang.c , (E-Mail Removed)
> >> (Chris) wrote:
> >>
> >> >void echo_off(void)

> >
> >> >return;
> >>
> >> Style point: you can't return from a void function.

> >
> >Wrong. You can't return _a value_ from a void function. You can
> >certainly return from it.

>
> Thats what I meant. The return is useless in this example.


Not quite; it has no effect on the compiled code, but it does have an
effect on the maintainer. It is, in any case, far from incorrect, and I
doubt many people would call it bad style. In fact, I'd call not having
it, well, not _bad_ style, but slightly off, certainly.

> >After all, how else would you return from it
> >prematurely?

>
> By using the One True Control Style. To misquote highlander: "there
> can /be/ only one return point".


Vade retro, Niklaus Wirth!

Richard
 
Reply With Quote
 
Chris
Guest
Posts: n/a
 
      10-15-2003
(E-Mail Removed) (Dan Pop) wrote in message news:<bmh6b5$srg$(E-Mail Removed)>...
> In <(E-Mail Removed) > (E-Mail Removed) (Chris) writes:
>
> > Well I would totally have to disagree. After searching around I found
> >snippet of code that can stop text from going to the console. Take a
> >look below. This is on a Linux Box. Turning off echoing in a root
> >shell with stty is easy but this worked using C.

>
> Well, you can always use stty in a C program (think system()) to avoid
> getting your hands dirty.
>
> >static struct termios stored_settings;
> >void echo_off(void)
> >{
> >struct termios new_settings;
> >tcgetattr(0,&stored_settings);
> >new_settings = stored_settings;
> >new_settings.c_lflag &= (~ECHO);
> >tcsetattr(0,TCSANOW,&new_settings);
> >return;
> >}

>
> Dan


So what everyone is saying is that this is not C ? Then what is it ?
It compiles as a .c program.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Viewstate wont turn off even after setting it to off ! robert112 ASP .Net 1 04-26-2007 01:51 AM
How to turn off input echo in irb? Jacklty Lam Ruby 4 08-19-2006 10:24 AM
how to switch off an echo from cin? andwing@gmail.com C++ 4 08-15-2006 01:48 PM
Turning echo off in Java ctippur@gmail.com Java 8 07-13-2005 11:28 AM
echo echo echo craig judd Computer Support 1 09-23-2003 08:53 PM



Advertisments