473,395 Members | 1,464 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.

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 31375
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:
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.