Stderror

General FreeBASIC programming questions.
Bunuel66
Posts: 76
Joined: May 19, 2006 19:56

Stderror

Postby Bunuel66 » Oct 02, 2008 21:59

I would like to use open pipe with a program sending it's output to sterr rather to stdout.
Is it some way to do that? (Open pipe seems not to be able to access stderr.)

Regards
anonymous1337
Posts: 5494
Joined: Sep 12, 2005 20:06
Location: California

Postby anonymous1337 » Oct 02, 2008 22:17

There's Open Error, which makes me unsure as to whether stderr can be accessed as a pipe. Looks like stdout might handle that.
Zippy
Posts: 1295
Joined: Feb 10, 2006 18:05

Postby Zippy » Oct 02, 2008 23:12

@Bunuel

Windows or *nix?

If Windows, I find that STDOUT and STDERR both appear as STDIN to open pipe with fb v.20. Using this test of STDERR:

Code: Select all

'compile as stderr-1.exe
#include once "crt.bi"
'
dim as integer res
res=fprintf(STDERR,!"Now is the time\n")
'
end

[You can test this to verify that it is printing only to STDERR]

And then this as test of open:

Code: Select all

dim as string s
open pipe "stderr-1.exe" for input as #1
while not eof(1)
   line input #1,s
   print s
wend
'
end

I get "Now is the time".
'
I expected to have to redirect STDERR in the pipe:
open pipe "stderr-1.exe 2>&1" for input as #1
but that isn't necessary for me on XP.
'
So my experience with Windows XP is that "open pipe" is capturing STDIN and STDERR as one (folding both into STDIN).
'
You are using what version of fb on what OS?
anonymous1337
Posts: 5494
Joined: Sep 12, 2005 20:06
Location: California

Postby anonymous1337 » Oct 02, 2008 23:31

I made a test as well.

StdErrTest: Opens up testprog, passes the name of stderrtest to the testprog, and then passes a second argument which will be printed through stderr by testprog.

Testprog: Gets the name of the exe that called it, opens up its output stream and prints data back to it as well as the stderr stream.

stderrtest.bas

Code: Select all

dim as integer ff = freefile()
dim as string tempStr

open pipe "testProg.exe stderrtest.exe foo" for input as #ff
   while not EOF(ff)
      line input #ff, tempStr
      print tempStr
   wend
close #ff
sleep


testProg.bas

Code: Select all

dim as integer fferr = freefile()
dim as integer ffout = freefile()

dim as string cmnd = command(1)
open err for output as #fferr
open pipe cmnd for output as #ffout
   print #ffout, "Hello, " & cmnd
   print #fferr, "RANDOM ERROR BECAUSE OF: " & command(2)
close #ffout
close #fferr
Zippy
Posts: 1295
Joined: Feb 10, 2006 18:05

Postby Zippy » Oct 03, 2008 14:47

After some thought.. STDERR is not wrapped in with STDIN in "open pipe", Bunuel66 is correct that "open pipe" doesn't capture STDERR. So it is necessary to redirect STDERR.

This is a better test, and reflects redirecting STDERR:

Code: Select all

dim as string s
open pipe "stderr-1.exe 2>&1" for input as #1
while not eof(1)
    line input #1,s
    if not eof(1) then
        color 12,0    'if isn't red STDERR wasn't redirected
        print "s= ";s 'if no "s= "..
    end if
wend
'
sleep
end
Bunuel66
Posts: 76
Joined: May 19, 2006 19:56

Still in trouble....

Postby Bunuel66 » Oct 03, 2008 20:03

Thanks guys for the suggestions. As said by zippy stderr is not handled by the pipe.
I have tried a lot of combinations without success.
To be more detailed, I I'm trying to catch the output of the java compiler "javac" for displaying it in an IUP window.
I'm running FBC under Ubuntu 8.04.1
I have found a work around using javac -XStdout for forcing the output to an intermediate file.
But no way to do it directly......If somebody has an idea....

Regards

Luis
Sisophon2001
Posts: 1704
Joined: May 27, 2005 6:34
Location: Cambodia, Thailand, Lao, Ireland etc.
Contact:

Postby Sisophon2001 » Oct 04, 2008 0:56

(an idea) If you are interested in Linux only, then you can use the C runtime functions.

Garvan
Bunuel66
Posts: 76
Joined: May 19, 2006 19:56

Postby Bunuel66 » Oct 11, 2008 14:01

After a lot of test and errors, a very simple way to capture the stderr file through a pipe:
Just call your command with 2>&1 \ before
Beware that the "\" is important for extending the redirection to all the following commands.

Regards

Return to “General”

Who is online

Users browsing this forum: Bing [Bot] and 10 guests