473,837 Members | 1,492 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 31409
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.flus h() # 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
5855
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 $descriptorspec = array( 0 => array("pipe", "r"), // stdin is a pipe that the child
1
4619
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 anyone have any ideas on how I can do this? Thanks for responses.
0
1838
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 an EOF. After some debugging the reason for this appears to be that the stdin and stdout that the ReadLine function in the tokeniser include are not affected by my redirection and refer to a non-terminal so don't stall waiting for input from my...
6
13679
by: chad kline | last post by:
FBSD 4.8/GCC //////// C-CODE: //////// char c; while ( 1 ) {
6
2488
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 confirm whether he really wants to overwrite the existing output file. The problem is that the second read from stdin, to obtain the user response whether to overwrite the existing output file, never waits for the user's response. It's as if a...
8
3901
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
4085
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 standard way to do the same without resorting to OS-specific API calls?
4
3254
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 variable line. This appears to be the behaviour when it's reading STDIN interactively (i.e. from the keyboard).
16
2892
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 ) { fwrite(linha,1,sizeof(linha),razor); } pclose(razor)
5
4389
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 in batch files where a file containing just the response would be redirected into the program so it wouldn't wait for user input. For example: program.exe < input.txt It seems as though redirected files don't act the same as user input in a...
0
9843
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10882
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10630
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10275
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7811
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7005
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4476
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4045
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3126
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.