[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Problems with popen3

Albert

4/18/2005 1:28:00 AM

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?
2 Answers

nobu.nokada

4/18/2005 11:02:00 AM

0

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


Albert

4/22/2005 5:19:00 PM

0

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.