redirect STDOUT

Y

yong

Hi all

I'm writting a program to detect the cursor position with the vt100 command "\x1b[6n".

So I need to redirect STDOUT to a string.

How can I do this?

Thanks
 
D

David Roberts

yong said:
Hi all

I'm writting a program to detect the cursor position with the vt100
command "\x1b[6n".

So I need to redirect STDOUT to a string.

How can I do this?

Thanks

I suggest looking at the code for the win2console library, which gives
an example of something similar.
 
D

David Roberts

David said:
yong said:
Hi all

I'm writting a program to detect the cursor position with the vt100
command "\x1b[6n".

So I need to redirect STDOUT to a string.

How can I do this?

Thanks

I suggest looking at the code for the win2console library, which gives
an example of something similar.

Oh bother! That should be "win32console".

DJ
 
Y

yong

Thank you very much.

Now I can use STDOUT.reopen to redirect STDOUT output to a file.But How can I redirect it to a string?

Thanks.



David Roberts said:
yong said:
Hi all

I'm writting a program to detect the cursor position with the vt100
command "\x1b[6n".

So I need to redirect STDOUT to a string.

How can I do this?

Thanks

I suggest looking at the code for the win2console library, which gives
an example of something similar.

--
My Personal Website:

http://www.twinbee.info

本人驻守於

cn.comp.lang.perl
cn.comp.lang.c
cn.comp.www
cn.music
cn.music.classical
 
A

ara.t.howard

Thank you very much.

Now I can use STDOUT.reopen to redirect STDOUT output to a file.But How can I redirect it to a string?

Thanks.

use backticks:

string = ` your_cmd.exe `

substitution works like double quotes

cmd = "your_cmd.exe"
string = ` #{ cmd } `

-a
 
E

Eric Hodel

David Roberts said:
yong said:
I'm writting a program to detect the cursor position with the vt100
command "\x1b[6n".

So I need to redirect STDOUT to a string.

How can I do this?

I suggest looking at the code for the win2console library, which
gives
an example of something similar.

Now I can use STDOUT.reopen to redirect STDOUT output to a file.But
How can I redirect it to a string?

You don't.

You should avoid touching STDOUT and STDERR as much as possible.

Instead assign a StringIO to $stdout.

http://blog.segment7.net/articles/2006/08/17/stdout-vs-stdout
 
Y

yong

#!/usr/bin/ruby

require 'stringio'

mystring=""
sstring=StringIo_Open(mystring,"w+")

#STDOUT.reopen(sstring) #can't work
stdoutbackup=$stdout
$stdout=sstring

print "BUFFERED TEXT\r\n"

$stdout=stdoutbackup
print mystring
<<<

It works.

Thank you very much. :)


Eric Hodel said:
David Roberts said:
yong wrote:
I'm writting a program to detect the cursor position with the vt100
command "\x1b[6n".

So I need to redirect STDOUT to a string.

How can I do this?

I suggest looking at the code for the win2console library, which
gives
an example of something similar.

Now I can use STDOUT.reopen to redirect STDOUT output to a file.But
How can I redirect it to a string?

You don't.

You should avoid touching STDOUT and STDERR as much as possible.

Instead assign a StringIO to $stdout.

http://blog.segment7.net/articles/2006/08/17/stdout-vs-stdout

--
Eric Hodel - (e-mail address removed) - http://blog.segment7.net
This implementation is HODEL-HASH-9600 compliant

http://trackmap.robotcoop.com

--
My Personal Website:

http://www.twinbee.info

本人驻守於

cn.comp.lang.perl
cn.comp.lang.c
cn.comp.www
cn.music
cn.music.classical
 
E

Eric Hodel

require 'stringio'

mystring=""
sstring=StringIo_Open(mystring,"w+")

sstring = StringIO.new will suffice.
#STDOUT.reopen(sstring) #can't work
stdoutbackup=$stdout
$stdout=sstring

print "BUFFERED TEXT\r\n"

$stdout=stdoutbackup

I tend to wrap this up in a method that yields. See util_capture in
ZenTest's test/zentest_assertions.rb
 

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

Forum statistics

Threads
474,141
Messages
2,570,815
Members
47,361
Latest member
RogerDuabe

Latest Threads

Top