Problems with popen3

I'm trying to wrap Ruby around a program, interfacing with it via
stdin/stdout. Say I have the following C program:

#include <stdio.h>
int main() {
  const char buf[128];
  while (1) {
    printf("enter: ");
    gets(buf);
    printf("result: %s\n", buf);
  }

  return 0;
}

And the following ruby script:

#!/usr/bin/env ruby
require 'open3'
gin, gout, gerr = Open3.popen3('./mytest.exe')
gout.sync = true
STDOUT.sync = true

while line = gout.gets
    puts 'recv:' + line
end

I expect it to print "recv: enter: " but it doesn't. At least on ruby
1.8.2 under cygwin under windows, Ruby blocks on the gout.gets. I've
tried adding STDOUT.sync = true and gout.sync = true but that doesn't
help. I tested it on Fedora/Ruby 1.8.2 and it doesn't seem to work there
either. Am I doing something wrong, or is this just a limitation of
popen?

Hi,

At Mon, 18 Apr 2005 10:29:43 +0900,
Albert wrote in [ruby-talk:138624]:

int main() {
  const char buf[128];
  while (1) {
    printf("enter: ");

This doesn't output a newline.

while line = gout.gets

IO#gets reads till a newline.

···

--
Nobu Nakada

nobu.nokada@softhome.net wrote in news:200504181101.j3IB1Guo029528
@sharui.nakada.niregi.kanuma.tochigi.jp:

Hi,

At Mon, 18 Apr 2005 10:29:43 +0900,
Albert wrote in [ruby-talk:138624]:

int main() {
  const char buf[128];
  while (1) {
    printf("enter: ");

This doesn't output a newline.

while line = gout.gets

IO#gets reads till a newline.

Ahh, thanks. I also had to flush stdout from the c program more often.