stdin -> stdout

M

max(01)*

hi.

i was wondering, what's the simplest way to echo the standard input to
the standard output, with no modification.

i came up with:

....
while True:
try:
raw_input()
except EOFError:
break
....

but i guess there must be a simpler way.

using bash i simply do 'cat', *sigh*!

bye

max

ps: in perl you ca do this:

....
while ($line = <STDIN>)
{
print STDOUT ("$line");
}
....
 
L

limodou

2005/8/19 said:
hi.

i was wondering, what's the simplest way to echo the standard input to
the standard output, with no modification.

i came up with:

...
while True:
try:
raw_input()
except EOFError:
break
...

but i guess there must be a simpler way.

using bash i simply do 'cat', *sigh*!

bye

max

ps: in perl you ca do this:

...
while ($line = <STDIN>)
{
print STDOUT ("$line");
}
...

Try this.

import sys

line = sys.stdin.readline()
while line:
sys.stdout.write(line)
line = sys.stdin.readline()
 
S

Steven Bethard

max(01)* said:
i was wondering, what's the simplest way to echo the standard input to
the standard output, with no modification.

import sys
for line in iter(sys.stdin.readline, ''):
sys.stdout.write(line)

Note that this uses the second form of iter(), which calls its first
argument repeatedly until it returns the sentinel value (its second
argument).

STeVe
 
S

Steven Bethard

import sys
for l in sys.stdin:
sys.stdout.write(l)

This is fine if you don't need the reads and writes of lines to run in
lockstep. File iterators read into a buffer, so you'll probably read
4096 bytes from stdin before you ever write a line to stdout. If that's
okay, this is a good solution. OTOH, if you want the reads and writes
to run in lockstep, you should probably use this idiom:

import sys
for line in iter(sys.stdin.readline, ''):
sys.stdout.write(line)

STeVe

P.S. You may also be able to get your version working using the -u
(unbuffered) option to Python, but I couldn't.
 
J

Jorgen Grahn

hi.

i was wondering, what's the simplest way to echo the standard input to
the standard output, with no modification.

i came up with: ....
but i guess there must be a simpler way.

using bash i simply do 'cat', *sigh*! ....
ps: in perl you ca do this:

...
while ($line = <STDIN>)
{
print STDOUT ("$line");
}
...

Actually, in perl it's easier than that, if you can tolerate that it also
filters file(s) given on the command-line:

while(<>) {
print;
}

or even:

#!/usr/bin/perl -p
;

Of course, not every programming language needs to have easy-to-use
filtering capabilities at the core.

/Jorgen
 
J

Jeff Schwab

max(01)* said:
i was wondering, what's the simplest way to echo the standard input to
the standard output, with no modification. ....
ps: in perl you ca do this:

...
while ($line = <STDIN>)
{
print STDOUT ("$line");
}
...

I guess you could, but there wouldn't be much point. In Perl, you can
do this with just command-line flags:

perl -pe '' input.txt

Or if you really want something to put in a file:

print <>

Here's the closest thing I could come up with in Python:

import sys
for line in sys.stdin:
print line,
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top