Detect if stdin is via keyboard or via < file?

S

srbstar

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
 
L

Lew Pitcher

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

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

CBFalconer

Lew said:
Not in standard C, it can't.

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


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

Chris Dollin

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.
 

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

Members online

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top