473,395 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Detect if stdin is via keyboard or via < file?

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
Nov 15 '05 #1
3 2220
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

sr*****@sbcglobal.net 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)

iD8DBQFDD7VAagVFX4UWr64RAi2yAJ9M7NGcb3z4zRVIkuJ8Eg 7WKaQdFQCfYBie
Wl2MOzXg/kxLYBvIVGwErRw=
=Fiqc
-----END PGP SIGNATURE-----
Nov 15 '05 #2
Lew Pitcher wrote:
sr*****@sbcglobal.net 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
Nov 15 '05 #3
sr*****@sbcglobal.net 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.
Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Scott Shaw | last post by:
Hi all, I was wondering if you could help out with this problem that I am having. What I am trying to do is detect keyboard input in a while loop without halting/pausing the loop until the key is...
2
by: Wodny | last post by:
Hello. Is it possible to return stdin to keyboard after redirecting it? For example after command "myprogram.exe < data.txt", when I finish reading from file I would like to be able to use...
7
by: | last post by:
Can I append a string to stdin? How? How can I read from stdin a whole line? because scanf("%s", string) reads only a word even if I use "" thanks
2
by: gc | last post by:
I was having trouble getting fgets to read a string from stdin, it was reading a '\n' already in the buffer. Someone told me to rewind stdin before calling fgets. It works, but is it permissible?
23
by: herrcho | last post by:
What's the difference between STDIN and Keyboard buffer ? when i get char through scanf, i type in some characters and press enter, then, where do the characters go ? to STDIN or Keyboard...
11
by: Darklight | last post by:
is this correct or incorrect i just read the post below before posting this: In fflush(stdin), what happens to flushed data? this program is taken from sams teach yourself c in 21 days /*...
11
by: David Warner | last post by:
Greetings! I need to write some C code that will decide between either reading from stdin or take a file name from argv and process it. The program needs to work like all of the typical unix...
30
by: sajjanharudit | last post by:
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...
9
by: Peter Blues | last post by:
I'm trying to write from keyboard input to a txt file. I don't understand why the while loop below doesn't achieve this. I.. 1: open the file for writing through FILE pointer outstream 2: I get...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.