Problems with popen3

A

Albert

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?
 
N

nobu.nokada

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.
 
A

Albert

(e-mail address removed) wrote in @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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top