Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

stdin help

34 views
Skip to first unread message

sajjan...@gmail.com

unread,
Jan 4, 2006, 11:13:34 AM1/4/06
to
i need to check the stdin, repeatedly for an input form the keyboard,
with out prompting the user to press a key or without returning pressed
key on screen..

now the problem is that if i use getc() the program execution stpos at
this line and waits for the user to press a key. cant use getchar
because the key pressed appears on the screen and moreover it is
buffered.

what i need is a small technique whereby which I can check the stdin
for an input, without prompting the user(the user will enter a key as
and when he wishes, not depending on the program) AND without halting
the execution (as getc() does) AND without displaying the key on the
screen AND should store the pressed key to variable

Nick Keighley

unread,
Jan 4, 2006, 11:25:59 AM1/4/06
to

you can't do this in standard C. Your platform (Unix, Windows etc.) may

provide a means to do this, Try asking on a platform specific news
group.


--
Nick Keighley

Alastair

unread,
Jan 4, 2006, 11:39:20 AM1/4/06
to

This is not so straightforward as you think it shoud be. I have
recently developed a simple program in C++ (I know, different language)
to do the same thing. I had to use a seperate thread (or process).

One process would do all the waiting (fgetc(stdin) amd all that), the
other process can periodically check on the results with a simple
"if(is_there_message_waiting())" type decision.

However it is not the same in C. You may need to write a seprate
communication handling program that stores the resulting messages in a
file (or other storage) - essentially a second process. This was one of
my trains of thought before I decided to use C++ for that particular
problem.

Interrupts is another method, but it all boils down to the same thing.

well, at least I can't think of another way....

Alastair

Chuck F.

unread,
Jan 4, 2006, 12:16:15 PM1/4/06
to
Alastair wrote:
> sajjan...@gmail.com wrote:
>
... snip ...

>>
>> what i need is a small technique whereby which I can check the
>> stdin for an input, without prompting the user(the user will
>> enter a key as and when he wishes, not depending on the
>> program) AND without halting the execution (as getc() does)
>> AND without displaying the key on the screen AND should store
>> the pressed key to variable
>
... snip ...

>
> One process would do all the waiting (fgetc(stdin) amd all
> that), the other process can periodically check on the results
> with a simple "if(is_there_message_waiting())" type decision.
>
... snip ...

>
> Interrupts is another method, but it all boils down to the same
> thing.

Please don't answer off-topic queries with off-topic material.
Limit your reply to suggestions as to where to look. The reason is
that the experts who can correct any errors you might make are not
here to make those corrections, so bad advice may not be caught.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>

Alastair

unread,
Jan 4, 2006, 1:17:57 PM1/4/06
to
>
> Please don't answer off-topic queries with off-topic material.
> Limit your reply to suggestions as to where to look. The reason is
> that the experts who can correct any errors you might make are not
> here to make those corrections, so bad advice may not be caught.
>

What!??!!
you're above comments are just not required here - please do not reply
unless you have somthing useful to add.

I was using the C++ example as an illustration of a way it could be
done, so that I could explain how you cannot easily do this in C
(therefore to do with the C language). Also if I think a solution can
be done better in a different language I won't hesitate to suggest it
(even if this is the C forum)...
And further - I'll answer anything that I can help with.

Alastair

Christopher Benson-Manica

unread,
Jan 4, 2006, 1:29:15 PM1/4/06
to
Alastair <adada...@gmail.com> wrote:

> And further - I'll answer anything that I can help with.

If you insist on answering off-topic questions, you could at least
cross-post to a group (or groups) where the answer is topical. If you
teach a man to ask questions in venues where the questions are
topical, he will never want for helpful suggestions. Your post
provided little in the way of such helpful instruction.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.

Robert Gamble

unread,
Jan 4, 2006, 1:32:55 PM1/4/06
to

There is no way to do what you are looking for in Standard C but many
platforms provide non-blocking input handling functions that will allow
you to check if there is input waiting and act accordingly. You will
need to check your implementation's documentation or post to a group
that deals with your platform for the details.

Robert Gamble

Robert Gamble

unread,
Jan 4, 2006, 1:37:03 PM1/4/06
to
Alastair wrote:
> >
> > Please don't answer off-topic queries with off-topic material.
> > Limit your reply to suggestions as to where to look. The reason is
> > that the experts who can correct any errors you might make are not
> > here to make those corrections, so bad advice may not be caught.
> >
>
> What!??!!
> you're above comments are just not required here - please do not reply
> unless you have somthing useful to add.

But his comments were useful, yours were not.

> I was using the C++ example as an illustration of a way it could be
> done, so that I could explain how you cannot easily do this in C
> (therefore to do with the C language).

It's pretty easy to say, "you can't do that in Standard C, check your
implementation's documentation for details" without going through the
completely-offtopic "illustration" you provided.

> Also if I think a solution can
> be done better in a different language I won't hesitate to suggest it
> (even if this is the C forum)...
> And further - I'll answer anything that I can help with.

But you are not helping. The OP asked how to do this in C, since this
cannot be done in Standard C any discussion of how to do this is
off-topic and the OP is best served by being directed to a group that
deals with his platform since he is more likely to find useful answers
from people who know what they are talking about.

Robert Gamble

Alastair

unread,
Jan 4, 2006, 1:37:41 PM1/4/06
to

Christopher Benson-Manica wrote:
> Alastair <adada...@gmail.com> wrote:
>
> > And further - I'll answer anything that I can help with.
>
> If you insist on answering off-topic questions, you could at least
> cross-post to a group (or groups) where the answer is topical. If you
> teach a man to ask questions in venues where the questions are
> topical, he will never want for helpful suggestions.

Fair enough....

>Your post
> provided little in the way of such helpful instruction.

Maybe, In your opinon, but I wasn't trying to help someone else with my
experience of the same problem (i.e. not you).
anyway a request for - please stop attacking me and apply your efforts
on helping answer the query...

Alastair

David Resnick

unread,
Jan 4, 2006, 1:47:33 PM1/4/06
to
> Alastair

There are ongoing disputes on topicality. Most of the regulars here
think this group is about the standard C language and that discussions
about non-portable constructs (such as threads) should be directed
to newsgroups where they are relevant. There are several reasons
for this, including:

1) standard C is a big enough topic to keep one group quite busy
2) maintaining focus on a limited topic helps keep expertise
within the group. Too big a focus is no focus.
3) errors are more likely to be caught/better advice given in groups
where relevant specialists hang out.

All that said, I personally have a tendancy to give advice I am
confident of while at at the same time redirecting the poster to
a group that is more appropriate for their specific needs. Some
think that is too much, as it encourages discussion of the off topic
part of the post, though setting follow-ups can repair that somewhat.
And some are not as friendly as they might be about pointing out the
above, causing hurt feelings and belligerence on the part of
the recipients of less than kind posts (I think this may explain
Kenny, though his upbringing may also be to blame there).

I think your advice was not particularly good here, in that you had no
idea what platform the user was using (his question gave no clue that
I could see). The ways to achieve what he wants are different on
Windows than on Linux, and no doubt different still a VAX or an
embedded system. Whether or not multiple threads/processes
is the way to do it would thus best be discussed in a more
appropriate forum.

> And further - I'll answer anything that I can help with.

Nobody can stop you from posting what you want on this or any
group, assuming you don't run afoul of abuse policies of your ISP or
posting route. What might happen is that regulars can and will
killfile you if they find you unreasonable, resulting in a loss of
audience when you have a question which the many experts on
the group (of which I am NOT one) could have helped you answer.

-David

sajjan...@gmail.com

unread,
Jan 4, 2006, 2:26:26 PM1/4/06
to
Can any one direct me to a group or discussion forum where the topic
could be "relevant". I am using linux as my platform.

tmp123

unread,
Jan 4, 2006, 2:31:54 PM1/4/06
to
sajjan...@gmail.com wrote:
> now the problem is that if i use getc() the program execution stpos at
> this line and waits for the user to press a key. cant use getchar
> because the key pressed appears on the screen and moreover it is
> buffered.
>
Hi,

As an starting point, can I suggest you to try the small program
below?.

Kind regards.

#include <stdio.h>
#include <sys/time.h>

struct timeval timeval_poll = {0,0};

int main ( void )
{
fd_set i;
int count=0;

FD_ZERO(&i);
FD_SET(1,&i);
while ( select(2,&i,NULL,NULL,&timeval_poll) == 0 )
{
printf("%d\n",count++);
FD_SET(1,&i);
}
printf("typed!\n");

return 0;
}

Keith Thompson

unread,
Jan 4, 2006, 2:49:27 PM1/4/06
to

Alastair, there are a number of good reasons why we try to avoid
off-topic discussions here. The matter has been discussed at great
length many many times, and I don't have time to go into the details
right now. Answering anything you can help with really isn't as
helpful as pointing the questioner to an appropriate newsgroup or
other resource. You can even jump over to another newsgroup to
continue the discussion in an environment full of experts on what the
questioner is asking about.

Consider the possibility that those of us who have been regulars in
this newsgroup for many years might actually know what we're doing.

--
Keith Thompson (The_Other_Keith) ks...@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.

Flash Gordon

unread,
Jan 4, 2006, 3:44:30 PM1/4/06
to
tmp123 wrote:
> sajjan...@gmail.com wrote:
>> now the problem is that if i use getc() the program execution stpos at
>> this line and waits for the user to press a key. cant use getchar
>> because the key pressed appears on the screen and moreover it is
>> buffered.
>>
> Hi,
>
> As an starting point, can I suggest you to try the small program
> below?.
>
> Kind regards.
>
> #include <stdio.h>
> #include <sys/time.h>

This is not a standard header.

> struct timeval timeval_poll = {0,0};

timeval is not part of stadnard C.

> int main ( void )
> {
> fd_set i;

<snip>

fd_set is not part of standard C. In fact, most of what you have written
is not standard C and not available on all platforms and so may not be
available on the OPs platform and even if it is may not be the
appropriate solution.

You have been reading this group long enough to have seen advice on
topicality, but once more, DON'T post off topic advice without at the
very least redirecting the discussion to a more appropriate group where
people that know about the matter will be able to review and comment on
your advice.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.

Flash Gordon

unread,
Jan 4, 2006, 3:49:08 PM1/4/06
to
sajjan...@gmail.com wrote:

Please provide context when posting, Google is not Usenet and people
might not have seen the post you are replying to., see
http://cfaj.freeshell.org/google/ instructions on how to post properly
through Google.

> Can any one direct me to a group or discussion forum where the topic
> could be "relevant". I am using linux as my platform.

comp.programmer.unix or comp.linux.development.apps might be a good
starting point, after checking their FAQ to see if your question is
answered there.

Chuck F.

unread,
Jan 4, 2006, 3:52:56 PM1/4/06
to
sajjan...@gmail.com wrote:
>
> Can any one direct me to a group or discussion forum where the
> topic could be "relevant". I am using linux as my platform.

Since you failed to provide any context, nobody has the slightest
idea what you want. For means of so doing, see my sig and the
reference therein below. Read those before making any further
response on this newsgroup.

Chuck F.

unread,
Jan 4, 2006, 3:56:18 PM1/4/06
to
tmp123 wrote:
> sajjan...@gmail.com wrote:
>
>> now the problem is that if i use getc() the program execution
>> stpos at this line and waits for the user to press a key. cant
>> use getchar because the key pressed appears on the screen and
>> moreover it is buffered.
>
> As an starting point, can I suggest you to try the small program
> below?.
>
> Kind regards.
>
> #include <stdio.h>
> #include <sys/time.h>
... snip rest ...

sys/time.h is not a part of standard C, which makes your entire
post off-topic.

Kenny McCormack

unread,
Jan 4, 2006, 4:31:17 PM1/4/06
to
In article <UqydnaD9pJO...@maineline.net>,
Chuck F. <cbfal...@maineline.net> wrote:
...

>Please don't answer off-topic queries with off-topic material.
>Limit your reply to suggestions as to where to look. The reason is
>that the experts who can correct any errors you might make are not
>here to make those corrections, so bad advice may not be caught.

And that would be a tragedy...

Christopher Benson-Manica

unread,
Jan 4, 2006, 4:32:45 PM1/4/06
to
Flash Gordon <sp...@flash-gordon.me.uk> wrote:

> comp.programmer.unix

ITYM comp.unix.programmer?

tmp123

unread,
Jan 4, 2006, 4:40:30 PM1/4/06
to

It is not an answer (a 5 lines program can not be), it is a pointer.
It is the OP who must decide if follow it and collect more information
about, if applicable, ... . In this way, and together with the groups
that has been pointed there are a starting point. Can be in this group
there are not BSD/POSIX/... specialist, but at least there are enough
knowledgment to point a few of posible continuation lines that unblocks
a situation and helps a colleague.

Pedro Graca

unread,
Jan 4, 2006, 5:19:08 PM1/4/06
to
sajjan...@gmail.com wrote:
> Can any one direct me to a group or discussion forum where the topic
> could be "relevant". I am using linux as my platform.

Please provide context to your post.
To me this looks like you're asking about "stdin" (in the subject of the
post) which, I believe, is relevant in comp.lang.c.

What I see is available for the next 30 days (~ to 2005-02-03) at
http://www.pixpond.com/1/os4mn.png


--
Mail to my "From:" address is readable by all at http://www.dodgeit.com/
== ** ## !! ------------------------------------------------ !! ## ** ==
TEXT-ONLY mail to the whole "Reply-To:" address ("My Name" <my@address>)
may bypass my spam filter. If it does, I may reply from another address!

Flash Gordon

unread,
Jan 4, 2006, 5:19:55 PM1/4/06
to
Christopher Benson-Manica wrote:
> Flash Gordon <sp...@flash-gordon.me.uk> wrote:
>
>> comp.programmer.unix
>
> ITYM comp.unix.programmer?

Oops. Thanks for the catch.

Flash Gordon

unread,
Jan 4, 2006, 5:28:48 PM1/4/06
to
tmp123 wrote:
> Flash Gordon wrote:
>> tmp123 wrote:
>>> sajjan...@gmail.com wrote:
>>>> now the problem is that if i use getc() the program execution stpos at
>>>> this line and waits for the user to press a key. cant use getchar
>>>> because the key pressed appears on the screen and moreover it is
>>>> buffered.
>>>>
>>> Hi,
>>>
>>> As an starting point, can I suggest you to try the small program
>>> below?.

<snip off topic answer.

>> You have been reading this group long enough to have seen advice on
>> topicality, but once more, DON'T post off topic advice without at the
>> very least redirecting the discussion to a more appropriate group where
>> people that know about the matter will be able to review and comment on
>> your advice.
>

> It is not an answer (a 5 lines program can not be), it is a pointer.

Which, as you don't know the OPs platform (it was not specified) could
be pointing in COMPLETELY the wrong direction.

> It is the OP who must decide if follow it and collect more information
> about, if applicable, ... . In this way, and together with the groups
> that has been pointed there are a starting point. Can be in this group
> there are not BSD/POSIX/... specialist, but at least there are enough
> knowledgment to point a few of posible continuation lines that unblocks
> a situation and helps a colleague.

Had you specified that it was a POSIX/BSD/whatever solution and pointed
the OP to an appropriate group then it would not have been so bad.
However, there are systems with select etc. where they ONLY work on
sockets, not on the standard input stream. Did you give any indication
that it might compile and appear to work until the critical moment that
the OP shows it to his/her boss? No. You gave an off topic response with
absolutely NO indication it was off topic, or that there are better
places to discus the best solution, or that it might compile but do
completely the wrong thing. That can not only be not helpful, it can
also be counter-productive both for this group (by encouraging
discussion of things that are off topic) and for the OP (by making the
OP try it, have problems getting it to work, and spend ages trying to
find out what is going with you giving POSIX answers when for all you
know the OP is using CPM.

Mark McIntyre

unread,
Jan 4, 2006, 6:02:06 PM1/4/06
to
On 4 Jan 2006 10:17:57 -0800, in comp.lang.c , "Alastair"
<adada...@gmail.com> wrote:

>>
>> Please don't answer off-topic queries with off-topic material.
>> Limit your reply to suggestions as to where to look. The reason is
>> that the experts who can correct any errors you might make are not
>> here to make those corrections, so bad advice may not be caught.
>>
>
>What!??!!

What do you mean "what"? Its pretty clear what Chuck said.

>you're above comments are just not required here - please do not reply
>unless you have somthing useful to add.

He added something very useful indeed - the information that the OP
should regard your post with suspicion since you might be talking
complete nonsense, but theres nobody here who can contradict you with
confidence.

Mark McIntyre
--

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----

Mark McIntyre

unread,
Jan 4, 2006, 6:05:51 PM1/4/06
to
On 4 Jan 2006 10:37:41 -0800, in comp.lang.c , "Alastair"
<adada...@gmail.com> wrote:

>anyway a request for - please stop attacking me

This will happen when you respect the rules of this group, and stop
posting offtopic responses.
As it happens, I don't think your original post was too wildly
offtopic but you've simply made matters worse by complaining.

>and apply your efforts on helping answer the query...

This has already been done - just because you don't like or don't
understand the answer, doesn't mean its not right.

eerok

unread,
Jan 4, 2006, 7:09:10 PM1/4/06
to
On Wed, 04 Jan 2006 22:19:55 +0000, Flash Gordon wrote:
> Christopher Benson-Manica wrote:
>> Flash Gordon <sp...@flash-gordon.me.uk> wrote:

>>> comp.programmer.unix

>> ITYM comp.unix.programmer?

> Oops. Thanks for the catch.

Another correction: "comp.os.linux.development.apps" (you
missed the "os") :-)

--
"The secret of being boring is to say everything." - Voltaire

Joe Wright

unread,
Jan 4, 2006, 8:46:54 PM1/4/06
to
Mark McIntyre wrote:
> On 4 Jan 2006 10:37:41 -0800, in comp.lang.c , "Alastair"
> <adada...@gmail.com> wrote:
>
>
>>anyway a request for - please stop attacking me
>
>
> This will happen when you respect the rules of this group, and stop
> posting offtopic responses.
> As it happens, I don't think your original post was too wildly
> offtopic but you've simply made matters worse by complaining.
>
>
>>and apply your efforts on helping answer the query...
>
>
> This has already been done - just because you don't like or don't
> understand the answer, doesn't mean its not right.
> Mark McIntyre

I love that signoff line,

"Just because you don't like or don't understand the answer, doesn't
mean its not right."

Can I use in a sig, please?

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---

Kenny McCormack

unread,
Jan 5, 2006, 6:13:47 AM1/5/06
to
In article <1136399823.3...@f14g2000cwb.googlegroups.com>,
Robert Gamble <rgam...@gmail.com> wrote:
...

>It's pretty easy to say, "you can't do that in Standard C, check your
>implementation's documentation for details" without going through the
>completely-offtopic "illustration" you provided.

But it is much more straightforward and less condescending to say:

Not portable. Can't discuss it here. Blah, blah, blah.

Says exactly the same thing as yours, but in a more pleasant manner.

Kenny McCormack

unread,
Jan 5, 2006, 6:16:38 AM1/5/06
to
In article <irkor19kffqgl3eov...@4ax.com>,
Mark McIntyre <markmc...@spamcop.net> wrote:
...

>He added something very useful indeed - the information that the OP
>should regard your post with suspicion since you might be talking
>complete nonsense, but theres nobody here who can contradict you with
>confidence.

Nonsense. In fact, all you are demonstrating is your upbringing - your
mother obviously never taught you that if you don't have anything nice to
say, say nothing at all.

For many reasons, most of the newbies posting here would be better off if
the so-called "regulars" kept their comments to themselves.

Kenny McCormack

unread,
Jan 5, 2006, 6:19:35 AM1/5/06
to
In article <_6idnSGqRb76qiHe...@maineline.net>,

Chuck F. <cbfal...@maineline.net> wrote:
>sajjan...@gmail.com wrote:
>>
>> Can any one direct me to a group or discussion forum where the
>> topic could be "relevant". I am using linux as my platform.
>
>Since you failed to provide any context, nobody has the slightest
>idea what you want.

Obviously not true, since several people already answered his question
(pointed him to various other groups).

You might like to take a look at:

http://m-w.com/dictionary/nobody

Herbert Rosenau

unread,
Jan 6, 2006, 1:49:50 PM1/6/06
to
On Wed, 4 Jan 2006 16:13:34 UTC, "sajjan...@gmail.com"
<sajjan...@gmail.com> wrote:

> i need to check the stdin, repeatedly for an input form the keyboard,
> with out prompting the user to press a key or without returning pressed
> key on screen..
>

> now the problem is that if i use getc() the program execution stpos at
> this line and waits for the user to press a key. cant use getchar
> because the key pressed appears on the screen and moreover it is
> buffered.
>

> what i need is a small technique whereby which I can check the stdin
> for an input, without prompting the user(the user will enter a key as
> and when he wishes, not depending on the program) AND without halting
> the execution (as getc() does) AND without displaying the key on the
> screen AND should store the pressed key to variable
>

Impossible mission with standard C. stdin is a stream assigned to
something nobody knows at runtime what it can be like a keyboard, a
file or something else.

Whenever you needs the behavior you descreibes you should ask the
system the program should run under. It may (or may even not) serve
you with an API thad does exactly that.

--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2 Deutsch ist da!

0 new messages