473,320 Members | 1,535 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,320 software developers and data experts.

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 31373
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Bernhard Kuemel | last post by:
Hi! I want to read/write commands and program input to/from /bin/bash several times before I close the stdin pipe. However, reading from cat hangs unless I first close the stdin pipe. <?php...
1
by: Lisa C | last post by:
Hi all. I am trying to set up a C++ program which will continuously read stdin from another applications stdout. I will continue reading until I read a message that will indicate to stop. Does...
0
by: lickspittle | last post by:
Hi, I have Python embedded with my other code, and when my other code opens a console and redirects stdout, stdin and stderr to it, then calls PyRun_InteractiveLoop, it immediately returns with...
6
by: chad kline | last post by:
FBSD 4.8/GCC //////// C-CODE: //////// char c; while ( 1 ) {
6
by: Charlie Zender | last post by:
Hi, I have a program which takes the output filename argument from stdin. Once the program knows the output filename, it tries to open it. If the output file exists, the program asks the user to...
8
by: orium69 | last post by:
Hi! how could i know if it was written something in stdin since the last time i read it? Tks...
31
by: Nikos Chantziaras | last post by:
Hello. Is there a way to check if the current process has an stdin handle? In the win32 API, one can do: _eof(_fileno(stdin)) Crucial here is that the above doesn't block. Is there a...
4
by: Adam Funk | last post by:
I'm using this sort of standard thing: for line in fileinput.input(): do_stuff(line) and wondering whether it reads until it hits an EOF and then passes lines (one at a time) into the...
16
by: fbertasso | last post by:
Hi, I´m opening stdin to get a file and pass it through a pipe. razor=popen ("/var/qmail/bin/razor-check -home=/var/qmail/razor", "w"); while( (ret=fread(linha,1,sizeof(linha),stdin) ) 0 ) {...
5
by: TazDev | last post by:
I'm trying to write a console program to exactly duplicate a much older dos-based program. This older program uses getch() to prompt a user for a response to a question. This program is also used...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.