funneling a file to stdin from the command line

U

Ulrich Eckhardt

c said:
I've been re-familiarizing myself with C syntax using gcc for the first
time. For lack of a better idea, I've been choosing exercises from K&R2
at random.

Note that K&R C is not standard but predates the standard in many ways and
even contains obsolete/deprecated things. Consult ACCU's website for
reviews of books to be sure.
How do I funnel a file to stdin from the command line?

It depends on your system, but since you mention control-Z I guess it's DOS
or something similar so

type input.txt | programm.exe

should work. Maybe (can't try that now) the syntax used by Unix shells works
too:

programm.exe < input.txt

Uli
 
W

Walter Roberson

c gordon liddy said:
If I have text in a file called a.a, how do I funnel that in on the command
line to imitate this? With stdout, I'll redirect so:
, once I want the output to have a more permanent existence than in a dos
window.
How do I funnel a file to stdin from the command line?

That depends upon the shell you are using, not upon anything in
the C language (which does not know about redirection.)

[OT]
The '2>' suggests that you are using one of the descendants
of the Bourne shell. If so, then you would use <a.a on the
command line to redirect input.

wordcount <a.a >text4.txt 2>text5.txt

For further information, read the manual pages about your shell
(if you can find manual pages for it, which might not be easy
on some operating systems.)
 
R

Richard Heathfield

c gordon liddy said:

With stdout, I'll redirect so:
, once I want the output to have a more permanent existence than in a dos
window.

How do I funnel a file to stdin from the command line?

In a vanilla Win32 console, it's:

yourprogram < yourdatafile.txt
 
C

c gordon liddy

I've been re-familiarizing myself with C syntax using gcc for the first
time. For lack of a better idea, I've been choosing exercises from K&R2 at
random. I think this strategy commends itself to the autodidact who has, in
years past, worked through it sequentially. What's more, I have 2 different
sets of answers at hand: a hard copy and Heathfield's solns at
http://clc-wiki.net/wiki/K&R2_solutions .

In exercise 6-4, we're to count words from stdin:
/*
Assumptions: input is on stdin, output to stdout.

Plan: read the words into a tree, keeping a count of how many we have,
allocate an array big enough to hold Treecount (WORD *)'s
walk the tree to populate the array.
qsort the array, based on size.
printf the array
free the array
free the tree
free tibet (optional)
free international shipping!
*/

I compile this into an executable called wordtree.exe. If I type wordtree
on the command line, it waits for every word I write until I give control-Z,
and then gives me the words and their frequency. So far so good.

If I have text in a file called a.a, how do I funnel that in on the command
line to imitate this? With stdout, I'll redirect so:
text4.txt 2>text5.txt
, once I want the output to have a more permanent existence than in a dos
window.

How do I funnel a file to stdin from the command line?

Thanks.
 
C

c gordon liddy

Richard Heathfield said:
c gordon liddy said:



In a vanilla Win32 console, it's:

yourprogram < yourdatafile.txt
Thanks, Richard and Walter. That had to be some kind of record for quick
and effective responses.

I can't believe I didn't think to turn the arrows around. It is a
shell-specific thing, but I'd bet it works on a lot of systems.

Happy Easter. I think in the U.K, you're already there. My dad would hide
everybody else's baskets in the living room, dining, study. He'd hide mine
in the crawlspace of the garage. I found it every year, though.
 
K

Keith Thompson

Ulrich Eckhardt said:
Note that K&R C is not standard but predates the standard in many ways and
even contains obsolete/deprecated things. Consult ACCU's website for
reviews of books to be sure.

To be clear, the term "K&R C" is generally used to refer to the
pre-standard language defined by the *first* edition of K&R. The OP
is using K&R2, which describes the language as defined by the 1989
ANSI standard (equivalent to the 1990 ISO standard). The newer 1999
ISO standard officially supersedes the older version, but has not yet
been as widely implemented as C90.

[...]
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top