stdin help

S

sajjanharudit

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
 
N

Nick Keighley

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

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.
 
A

Alastair

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

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
 
C

Chuck F.

Alastair said:
(e-mail address removed) wrote:
.... snip ...
.... 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/>
 
A

Alastair

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
 
C

Christopher Benson-Manica

Alastair said:
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.
 
R

Robert Gamble

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

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
 
R

Robert Gamble

Alastair said:
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
 
A

Alastair

Christopher said:
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
 
D

David Resnick

Alastair said:
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)...

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
 
S

sajjanharudit

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

tmp123

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;
}
 
K

Keith Thompson

Alastair said:
Fair enough....


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, 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.
 
F

Flash Gordon

tmp123 said:
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.
 
F

Flash Gordon

(e-mail address removed) 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.
 
C

Chuck F.

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.

--
"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/>
 
C

Chuck F.

tmp123 said:
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.

--
"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/>
 
K

Kenny McCormack

Chuck F. said:
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...
 
T

tmp123

Flash said:
This is not a standard header.


timeval is not part of stadnard C.


<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.
--

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top