stdout

c++: subprocess output to stdin

不羁的心 提交于 2019-11-29 09:39:24
Suppose I want to call a subprocess from within my program, and I want to read the output from that subprocess into my program. Here is a trivial way to do that: //somefile.cpp system("sub_process arg1 arg2 -o file.out"); //call the subprocess and have it write to file FILE *f = std::fopen("file.out", "r"); //.... and so on We all know that i/o operations are computationally slow. To speed this up, I would like to skip the write-to-file-then-read-from-file step, and instead redirect the output of this sub-process directly into stdin (or some other stream) How would I do this? How do I skip the

Internally capture/redirect stdout?

余生颓废 提交于 2019-11-29 07:37:35
This seems like a bit of a computing systems 101 question, but I'm stumped. I am integrating existing code from C/C++ project A into my own project B. Both A and B will be linked into a single executable, threaded process. Project A's code makes extensive use of printf for output. This is fine, but I want also to capture that output into my own buffers. Is there a way I can read from stdout once the printf calls have written to it? I cannot fork the process or pipe. And my efforts to poll() stdout, or to dup() it, have not succeeded (I may be doing something wrong here). You can use freopen to

Redirect stdout and stderr from inside a batch file

柔情痞子 提交于 2019-11-29 07:30:17
问题 Is there a way to redirect stdout and stderr for a batch file from inside it. I'm imagining something like set STDOUT=stdout.log echo Some text a.exe b.exe c.exe Where both Some text , and the output of a.exe , b.exe and c.exe would go to stdout.log Is this possible? 回答1: It is more efficient to redirect once for the entire collection of commands than it is to redirect (with append) each individual command. It takes time to intialize the redirection. It may not be noticable for a few

Creating a custom sys.stdout class?

。_饼干妹妹 提交于 2019-11-29 07:20:59
What I'm trying to do is simply have the output of some terminal commands print out to a wx.TextCtrl widget. I figured the easiest way to accomplish this is to create a custom stdout class and overload the write function to that of the widget. stdout class: class StdOut(sys.stdout): def __init__(self,txtctrl): sys.stdout.__init__(self) self.txtctrl = txtctrl def write(self,string): self.txtctrl.write(string) And then I would do something such as: sys.stdout = StdOut(createdTxtCtrl) subprocess.Popen('echo "Hello World!"',stdout=sys.stdout,shell=True) What results is the following error:

Write to terminal after redirecting stdout to a file without using stderr?

徘徊边缘 提交于 2019-11-29 07:01:39
I have two shell scripts, one that serves as the main "program" and another that serves as a "library." In several places in the "program," I'll do something like: log "$thing" >> "$logfile" , where log is a function defined in the "library." # program.sh logfile="log.txt" stuff="hahah heheh hoho" . library.sh for thing in $stuff; do log "$thing" >> "$logfile" done My question: Is there a way to redirect some of the output from the function back to the terminal without using stderr ? # library.sh log () { # This gets written to the log echo "`date --rfc-3339=seconds`: $1" # How to write this

What does it mean to write to stdout in C?

别等时光非礼了梦想. 提交于 2019-11-29 06:06:34
问题 Does a program that writes to "stdout" write to a file? the screen? I don't understand what it means to write to stdout. 回答1: That means that you are printing output on the main output device for the session... whatever that may be. The user's console, a tty session, a file or who knows what. What that device may be varies depending on how the program is being run and from where. The following command will write to the standard output device (stdout)... printf( "hello world\n" ); Which is

Python - reset stdout to normal, after previously redirecting it to a file

那年仲夏 提交于 2019-11-29 06:04:55
问题 At the beginning of my python program I have the following line: sys.stdout = open('stdout_file', 'w') Halfway through my program I would like to set stdout back to the normal stdout. How do I do this? 回答1: The original stdout can be accessed as sys.__stdout__ . This is documented. 回答2: The same holds for stderr, of course. At the end, these lines are needed to get the original streams. sys.stdout = sys.__stdout__ sys.stderr = sys.__stderr__ 回答3: Another common practice is to store the

How do I detect if stdout is connected to a tty in Perl?

↘锁芯ラ 提交于 2019-11-29 05:49:59
I'm looking for the Perl equivalent to this Python code: from sys import stdout if stdout.isatty(): print "yes" else: print "no" Use the -t filetest operator . print -t STDOUT ? "Yes\n" : "No\n" Note that in Perl, STDOUT can be tied (essentially an overcomplicated overloaded object) so output to STDOUT may still reach a TTY even if its not directly attached to one. Use IO::interactive if you require STDOUT to actually be connected to the terminal, and not just being redirected to /dev/null/ or whatever. 来源: https://stackoverflow.com/questions/3517250/how-do-i-detect-if-stdout-is-connected-to-a

Merge and sync stdout and stderr?

岁酱吖の 提交于 2019-11-29 05:10:17
say I'm running an exe from a python script using: subprocess.call(cmdArgs,stdout=outf, stderr=errf) when outf and errf are file descriptors of text files. is there any way I can generate on top of it a merged and synced text file of both stdout and stderr? it should be formatted with time and source(our/err). thanks It is a bit tricky, since you need to poll the stdout and stderr file descriptors of the subprocess while it's running, to get accurate timestamps. You also need to chop up the output into a list of lines so the final results can be merged and sorted easily. You could easily merge

How do I chain stdout in one child process to stdin in another child in C?

蹲街弑〆低调 提交于 2019-11-29 05:09:23
I've been messing around in C trying to figure out how to do this. Let's say I have my main program, the parent process. The parent creates three child processes, each of which will eventually run programs (but that's not important right now). What I'd like to do is make it so that the first child's stdout will be received by the second child's stdin. The second child's stdout will then be received by the third child's stdin. The parent process's stdin/stdout aren't messed with at all. So far, what I've got is pipe(procpipe); parentPid = getpid(); for(i = 0; i < 3; i++) { if(getpid() ==