469,366 Members | 2,342 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,366 developers. It's quick & easy.

Reading from stdin

Dear All,

I have a C program that prints out numbers 1 to 10 to stdout. I wish to pipe
this into a python script to print out the numbers, e.g. ./myCProg |
myPython.py

However, I want the python script to print out the numbers as it gets them,
since there could be hours/days between numbers - not a very efficient C
program :). Actually, I would like to read-a-line/ print-a-line/
read-a-line/etc

I have tried things like 'sys.stdin.read*' with no success.

Thanks

Colin
Jul 18 '05 #1
4 31134
C GIllespie wrote:
efficient C program :). Actually, I would like to read-a-line/
print-a-line/ read-a-line/etc


probably both your C- and your python scripts use buffered output. So the
C-program will not print line-by-line but rather en-block after many bytes
of output have accumulated.

In your C-program, do this (that's most likely your problem):

printf("%f\n",x); /* print valuable data */
fflush(stdout); /* make sure it's written to stdout */

In your python-program, do this (that's probably not your problem):

print "%f"%x # print valueable data
sys.stdout.flush() # make sure it's written to stdout

Chris
Jul 18 '05 #2

Colin> However, I want the python script to print out the numbers as it
Colin> gets them, since there could be hours/days between numbers - not
Colin> a very efficient C program :). Actually, I would like to
Colin> read-a-line/ print-a-line/ read-a-line/etc

Colin> I have tried things like 'sys.stdin.read*' with no success.

Have you tried this?

import sys
for line in sys.stdin:
print line.strip()

Skip

Jul 18 '05 #3
> probably both your C- and your python scripts use buffered output. So
the C-program will not print line-by-line but rather en-block after many bytes of output have accumulated. In your C-program, do this (that's most likely your problem): printf("%f\n",x); /* print valuable data */ fflush(stdout); /* make sure it's written to stdout */


Yep, that was the problem.

Thanks

Colin
Jul 18 '05 #4
P
Skip Montanaro wrote:
Colin> However, I want the python script to print out the numbers as it
Colin> gets them, since there could be hours/days between numbers - not
Colin> a very efficient C program :). Actually, I would like to
Colin> read-a-line/ print-a-line/ read-a-line/etc

Colin> I have tried things like 'sys.stdin.read*' with no success.

Have you tried this?

import sys
for line in sys.stdin:
print line.strip()


That's block buffered. More details here:
http://www.pixelbeat.org/readline/python

Pádraig.
Jul 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by lickspittle | last post: by
6 posts views Thread by chad kline | last post: by
6 posts views Thread by Charlie Zender | last post: by
8 posts views Thread by orium69 | last post: by
31 posts views Thread by Nikos Chantziaras | last post: by
4 posts views Thread by Adam Funk | last post: by
16 posts views Thread by fbertasso | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.