Thread: Grabbing stdin

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    98

    Grabbing stdin

    I've tried searching for this but was surprised not to find. If it is a repeat query please forgive me!!

    Can anybody tell me how to grab stdin before it gets to stdout? I would like to read what is being typed without it echoing to the screen, play with the input, then display it later.

    I've been looking at freopen to redirect stdout into a buffer, but I can't get it to work.

    Thanks.

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Non standard but easiest way to do this is to use getch(). This is a function which is not in the standard but most compilers provide it. On msvc its _getch(). It returns an int which holds the value of the keypress. It is unbuffered and doesnt echo to the screen so a loop will be needed with maybe a putchar('*') to print asterix for every keypress. Check your help files to see if you have getch() and how to use it.

    edit1 sorry note to self. dont roll joint and answer question at same time. takes too long!
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    98
    I was actually trying to do this on Unix...

    getch is implemented in the curses.h file, and I tried using it (after an initscr(), but it still echoed stdin to the screen aswell as to the variable ( I was trying to replace the input keystrokes with an asterix and ended up with an output that looked like a*b*c*...! ).

    I was also hoping for something which didn't involve screen manipulation...

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    98
    Thanks, that's rather impressive!

    Something you may be interested to hear though... to test, my main code is literally

    while( ch = mygetch())
    {
    putchar( '*' );
    }

    and while the keystrokes are replaced by the asterix, they only print out every fourth keystroke. (?!)

    I'm not complaining or anything, but I can't see why that would happen. The behaviour is consistent over a number of tests...

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    98
    fflush after putchar has had no effect.

    The program is waiting at the

    ch = getchar();

    line, and only continuing once four keystrokes have been entered.

    I'm compiling on Solaris7. I wonder if its a bug...

  6. #6
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946

    Re: Grabbing stdin

    Originally posted by Morgan
    I've tried searching for this but was surprised not to find. If it is a repeat query please forgive me!!

    Can anybody tell me how to grab stdin before it gets to stdout? I would like to read what is being typed without it echoing to the screen, play with the input, then display it later.

    I've been looking at freopen to redirect stdout into a buffer, but I can't get it to work.

    Thanks.
    there is a flaw in your reasoning: input from stdin is NOT echoed to stdout. a conforming implementation may echo it to a console or whatever it wishes to, but NOT to stdout. stdin doesnt "go" to stdout on its own.
    hello, internet!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Replies: 2
    Last Post: 07-12-2008, 12:00 PM
  3. value of stdin (not zero?)
    By taisao in forum Linux Programming
    Replies: 3
    Last Post: 05-27-2007, 08:14 AM
  4. Question About Reading Lines from stdin
    By Zildjian in forum C Programming
    Replies: 7
    Last Post: 11-12-2003, 05:45 AM
  5. stdin question
    By oomen3 in forum C Programming
    Replies: 6
    Last Post: 02-19-2003, 02:52 PM