Detect if stdin is via keyboard or via < file?

Discussion in 'C Programming' started by srbstar@sbcglobal.net, Aug 27, 2005.

  1. Guest

    Can a program detect if it's stdin is
    coming from keyboard input vs. being
    redirected from a file?

    If it's from a file, I want to fputs()
    the text. But if it's from the keyboard,
    I won't (don't want it typed, then
    fputs()'d.

    SRB
     
    , Aug 27, 2005
    #1
    1. Advertising

  2. Lew Pitcher Guest

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    wrote:
    > Can a program detect if it's stdin is
    > coming from keyboard input vs. being
    > redirected from a file?


    Not in standard C, it can't.

    But, with a platform-specific extension, it often can.

    > If it's from a file, I want to fputs()
    > the text. But if it's from the keyboard,
    > I won't (don't want it typed, then
    > fputs()'d.


    You'll either have to resort to some sort of signal data (like a command
    argument), or use a platform-specific extension to determine whether stdin
    comes from a file or an interactive device.


    - --
    Lew Pitcher

    Master Codewright & JOAT-in-training | GPG public key available on request
    Registered Linux User #112576 (http://counter.li.org/)
    Slackware - Because I know what I'm doing.
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.7 (GNU/Linux)

    iD8DBQFDD7VAagVFX4UWr64RAi2yAJ9M7NGcb3z4zRVIkuJ8Eg7WKaQdFQCfYBie
    Wl2MOzXg/kxLYBvIVGwErRw=
    =Fiqc
    -----END PGP SIGNATURE-----
     
    Lew Pitcher, Aug 27, 2005
    #2
    1. Advertising

  3. CBFalconer Guest

    Lew Pitcher wrote:
    > wrote:
    >
    >> Can a program detect if it's stdin is coming from keyboard input
    >> vs. being redirected from a file?

    >
    > Not in standard C, it can't.
    >
    > But, with a platform-specific extension, it often can.
    >
    >> If it's from a file, I want to fputs() the text. But if it's from
    >> the keyboard, I won't (don't want it typed, then fputs()'d.

    >
    > You'll either have to resort to some sort of signal data (like a
    > command argument), or use a platform-specific extension to
    > determine whether stdin comes from a file or an interactive device.


    However, on most unixlike systems there are methods available,
    which are OT here. The OP can examine one such in my id2id-20,
    which is available at:

    <http://cbfalconer.home.att.net/download/id2id-20.zip>

    --
    "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
     
    CBFalconer, Aug 27, 2005
    #3
  4. Chris Dollin Guest

    wrote:

    > Can a program detect if it's stdin is
    > coming from keyboard input vs. being
    > redirected from a file?
    >
    > If it's from a file, I want to fputs()
    > the text. But if it's from the keyboard,
    > I won't (don't want it typed, then
    > fputs()'d.


    I found the most straightforward tactic in a similar situation
    (whether to issue a prompt or not, for an interactive interpreter)
    to be a command-line flag. The program no longer needs to be able
    to answer the question.

    --
    Chris "electric hedgehog" Dollin
    Almost all predictions about life in 2015 are wrong in some fundamental
    respect.
     
    Chris Dollin, Aug 31, 2005
    #4
    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. Wodny

    Return stdin to keyboard

    Wodny, May 12, 2005, in forum: C++
    Replies:
    2
    Views:
    427
    Wodny
    May 12, 2005
  2. herrcho
    Replies:
    23
    Views:
    1,318
  3. Johnathan Doe

    peek at stdin, flush stdin

    Johnathan Doe, May 15, 2004, in forum: C Programming
    Replies:
    5
    Views:
    25,041
    Chatoyer
    May 17, 2013
  4. Peter Blues

    stdin (keyboard) to file

    Peter Blues, Feb 6, 2008, in forum: C Programming
    Replies:
    9
    Views:
    599
    CBFalconer
    Feb 6, 2008
  5. Mad Scientist Jr
    Replies:
    5
    Views:
    401
    Mad Scientist Jr
    Apr 17, 2006
Loading...

Share This Page