stdin help

Discussion in 'C Programming' started by sajjanharudit@gmail.com, Jan 4, 2006.

  1. Guest

    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
     
    , Jan 4, 2006
    #1
    1. Advertising

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


    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
     
    Nick Keighley, Jan 4, 2006
    #2
    1. Advertising

  3. Alastair Guest

    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


    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
     
    Alastair, Jan 4, 2006
    #3
  4. Chuck F. Guest

    Alastair wrote:
    > 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/>
     
    Chuck F., Jan 4, 2006
    #4
  5. Alastair Guest

    >
    > 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
     
    Alastair, Jan 4, 2006
    #5
  6. Alastair <> 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.
     
    Christopher Benson-Manica, Jan 4, 2006
    #6
  7. 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


    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, Jan 4, 2006
    #7
  8. 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
     
    Robert Gamble, Jan 4, 2006
    #8
  9. Alastair Guest

    Christopher Benson-Manica wrote:
    > Alastair <> 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
     
    Alastair, Jan 4, 2006
    #9
  10. 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.
    >
    > 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
     
    David Resnick, Jan 4, 2006
    #10
  11. Guest

    Can any one direct me to a group or discussion forum where the topic
    could be "relevant". I am using linux as my platform.
     
    , Jan 4, 2006
    #11
  12. tmp123 Guest

    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;
    }
     
    tmp123, Jan 4, 2006
    #12
  13. "Alastair" <> writes:
    > Christopher Benson-Manica wrote:
    >> Alastair <> 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, 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) <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.
     
    Keith Thompson, Jan 4, 2006
    #13
  14. Flash Gordon Guest

    tmp123 wrote:
    > 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, Jan 4, 2006
    #14
  15. Flash Gordon Guest

    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.
    --
    Flash Gordon
    Living in interesting times.
    Although my email address says spam, it is real and I read it.
     
    Flash Gordon, Jan 4, 2006
    #15
  16. Chuck F. Guest

    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.

    --
    "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/>
     
    Chuck F., Jan 4, 2006
    #16
  17. Chuck F. Guest

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

    --
    "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/>
     
    Chuck F., Jan 4, 2006
    #17
  18. In article <>,
    Chuck F. <> 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...
     
    Kenny McCormack, Jan 4, 2006
    #18
  19. Flash Gordon <> wrote:

    > comp.programmer.unix


    ITYM comp.unix.programmer?

    --
    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.
     
    Christopher Benson-Manica, Jan 4, 2006
    #19
  20. tmp123 Guest

    Flash Gordon wrote:
    > tmp123 wrote:
    > > 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.
    > --


    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.
     
    tmp123, Jan 4, 2006
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Johnathan Doe

    peek at stdin, flush stdin

    Johnathan Doe, May 15, 2004, in forum: C Programming
    Replies:
    5
    Views:
    25,443
    Chatoyer
    May 17, 2013
  2. Charlie Zender

    Reading stdin once confuses second stdin read

    Charlie Zender, Jun 19, 2004, in forum: C Programming
    Replies:
    6
    Views:
    822
    Dan Pop
    Jun 21, 2004
  3. Ben
    Replies:
    2
    Views:
    1,398
    jacob navia
    Aug 29, 2009
  4. Terry Cooper
    Replies:
    7
    Views:
    449
    Janos Sebok
    Jun 9, 2009
  5. Stefano Sabatini
    Replies:
    6
    Views:
    311
    Stefano Sabatini
    Jul 29, 2007
Loading...

Share This Page