As mentioned in one of the replies above, if the C program does not flush its stdout, its output will be buffered until the pipe connection has accumulated (I think) 8 KB or so of data (or else has finished its work), before the downstream process will get to see any data.

(When output goes directly to the terminal window, stdout does not get buffered -- the buffering applies when the output is going to a pipe or file. You could test this out by running your C program at the command line like this:

./myC.exe 10 10 1 abc > test.out
Then use another window to watch the size of the "test.out" file. If the C code is not flushing stdout at each printf statement, the file won't grow until the program has written 8K of data, or until it finishes, whichever comes first.)

And apart from the output buffering in the C program, if the strings being printed to stdout do not end in a line-feed, the perl script might still not print anything until it detects and EOF on the pipe. The default input record separator in perl is "\n" (with windows CRLF handled as needed in the default windows/perl setup) -- (update:) perl will keep reading into $_ on the first iteration of  while (<>) until it gets a line-feed or EOF.

When people do those kinds of progress-report string outputs, they often terminate each print with "\r", so that each iteration overwrites the previous string. If your C program is doing that, have your perl script use "\r" as the input record separator (the perl-internal global variable "$/") -- update: in other words, do this:  $/ = "\r";

And sometimes, the C code has been written so that the status output has no termination at all -- each iteration simply appends onto one line. If your C program is set up that way and you want to keep that "feature", you'll want to modify the C code to at least use some distinctive character (or string) at the end of each report iteration, so the perl script can use that character (or string) as the input record separator.


In reply to Re: Pipe Problem by graff
in thread Pipe Problem by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.