# Stdin/stdout/stderr problem in g++ and rails

**URL:** <https://www.ruby-forum.com/t/stdin-stdout-stderr-problem-in-g-and-rails/192492>\
**Category:** Rails\
**Created:** [July 31, 2010, 2:13pm UTC](https://www.ruby-forum.com/t/stdin-stdout-stderr-problem-in-g-and-rails/192492 "2010-07-31T14:13:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![silver-spoon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aeb1de/32.png) [@silver-spoon](https://www.ruby-forum.com/u/silver-spoon)\
**Post date:** [July 31, 2010, 2:13pm UTC](https://www.ruby-forum.com/t/stdin-stdout-stderr-problem-in-g-and-rails/192492/1 "2010-07-31T14:13:12Z")

</div>

In my rails controller, I am doing the following :

@output = `g++ j.cpp -o "prog" && ./prog`

This gives the output in the @output variable which i can display in  
my view. But the above works only if the j.cpp is correct and doesn’t  
expect any user input. How can I use the stdin/stderr and stdout  
streams here so that :

If the user has to give input, I open a dialog box on the view with a  
textfield where he/she can enter the input, and the program continues  
to execute.  
If there are any errors in the file, then I should be able to get the  
errors and display them to the user.  
I tried doing this :

@output = `g++ j.cpp -o "prog" && ./prog| tee prog`

This allows me to enter the user input at the server command prompt  
(the server log…i don’t know what do we call it), but I want it to  
be entered in a textfield in the view. Help me.

---

<div class="post-metadata">

**Author:** ![silver-spoon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aeb1de/32.png) [@silver-spoon](https://www.ruby-forum.com/u/silver-spoon)\
**Post date:** [August 2, 2010, 4:01pm UTC](https://www.ruby-forum.com/t/stdin-stdout-stderr-problem-in-g-and-rails/192492/2 "2010-08-02T16:01:19Z")

</div>

On Sat, Jul 31, 2010 at 08:12, Silver S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> In my rails controller, I am doing the following :
> 
> @output = `g++ j.cpp -o "prog" && ./prog`
> 
> This gives the output in the @output variable which i can display in  
> my view. But the above works only if the j.cpp is correct and doesn’t  
> expect any user input. How can I use the stdin/stderr and stdout  
> streams here so that :
> 
> If the user has to give input, I open a dialog box

Sorry, can’t help you there, at least nothing springs immediately to my  
mind.

> If there are any errors in the file, then I should be able to get the  
> errors and display them to the user.  
> I tried doing this :
> 
> @output = `g++ j.cpp -o "prog" && ./prog| tee prog`

There’s probably some more Rubyish way to do it by messing with the  
definitions of stout and stderr, but a quick and dirty way to do it,  
by messing with the the command line to make stderr go to stdout, is:

@output = `g++ j.cpp -o "prog" 2&>1`

Check $? to see if it succeeded (should be 0; make sure you check  
IMMEDIATELY after the command). if so, then you can run prog as  
normal.

This is an old shell-scripting type kluge that I wasn’t sure would  
work, but does. Proof-of-concept code:

#! /usr/bin/ruby  
out = `ls -l #{ARGV.join ' '} 2>&1`  
status = $?  
puts “command %sed (status %d)” % [(status == 0 ? ‘work’ : ‘fail’),  
status]  
puts out # insert your own joke here, folks!

Also be VERY careful about how you are feeding the program name into  
the command, especially if there’s any way a user can influence it.  
Google “SQL Injection”; it’s not just for SQL.

-Dave

–  
Specialization is for insects. -RAH | Have Pun, Will Babble! -me  
Programming Blog: [http://codosaur.us](http://codosaur.us) | Work: [http://davearonson.com](http://davearonson.com)  
Leadership Blog: [http://dare2xl.com](http://dare2xl.com) | Play: [http://davearonson.net](http://davearonson.net)

- 
  - 
    - 
      - 
        - WATCH THIS SPACE \* \* \* \* \* | Ruby: [http://mars.groupsite.com](http://mars.groupsite.com)
