Re: system() function
- Posted by Robert Craig <rds at ATTCANADA.NET> Dec 17, 1999
- 495 views
Brian Broker writes: > I know this has been brought up before but couldn't find > a solution. I'd like to capture output of a DOS program > without sending the output to a file. > Is there a way to do this? (a library with a system() function > that captures the output and lets you assign it to a > sequence variable) > Are there plans on adding this functionality to the next > release of Euphoria? As you are probably aware, you can already say things like: system("myprog > myoutput", 2) to get the output of "myprog" into a file called "myoutput". I guess what you want is to get the output back as a sequence of characters, or sequence of lines of characters, without ever creating a temporary file. I don't know of anything special that I can do in the interpreter to capture the output without creating a file. I don't plan to add a library routine for this, but here's something I just wrote. It uses a temporary file. I tested it lightly. function command_output(sequence command) -- return the output of a command -- as a sequence of characters integer fn, c sequence result system(command & " > cmdtemp.dat", 2) result = "" fn = open("cmdtemp.dat", "r") if fn = -1 then return result end if while 1 do c = getc(fn) if c = -1 then exit end if result = append(result, c) end while close(fn) system("del cmdtemp.dat", 2) return result end function -- TEST sequence r r = command_output("dir *.ex") puts(1, r) Regards, Rob Craig Rapid Deployment Software http://www.RapidEuphoria.com