Reading from stdin

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • C GIllespie

    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


  • Christian Vogel

    #2
    Re: Reading from stdin

    C GIllespie wrote:[color=blue]
    > efficient C program :). Actually, I would like to read-a-line/
    > print-a-line/ read-a-line/etc[/color]

    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


    Comment

    • Skip Montanaro

      #3
      Re: Reading from stdin


      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

      Comment

      • C GIllespie

        #4
        Re: Reading from stdin

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

        Yep, that was the problem.

        Thanks

        Colin


        Comment

        • P@draigBrady.com

          #5
          Re: Reading from stdin

          Skip Montanaro wrote:[color=blue]
          > 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()[/color]

          That's block buffered. More details here:


          Pádraig.

          Comment

          Working...