stdout

How to redirect the output of system() to a file?

自古美人都是妖i 提交于 2019-12-18 16:57:21
问题 In this C program #include <stdio.h> #include <fcntl.h> int main() { int file = open("Result", O_CREAT|O_WRONLY, S_IRWXU); dup2(stdout, file); system("ls -l"); return 0; } I'm trying to redirect the output of system() to a file, for that i have used dup2 but it is not working. What's wrong with this code ? and, please tell me if there is any better way to do this ? (without using > at the terminal ) 回答1: stdout is a FILE * pointer of the standard output stream. dup2 expects file descriptor,

launch app, capture stdout and stderr in c++

梦想与她 提交于 2019-12-18 16:34:33
问题 How do I launch an app and capture the output via stdout and maybe stderr? I am writing an automated build system and I need to capture the output to analyze. I'd like to update the svn repo and grab the revision number so I can move the files in autobuild/revNumber/ if successful. I also would like to build using make and upload the compile text to my server for everyone to see the warnings and errors on a failed build. I can't find the system() function, but I found the CreateProcess()

How can I print to console while the program is running in python? [duplicate]

混江龙づ霸主 提交于 2019-12-18 15:51:30
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: How to flush output of Python print? I have have an algorithm that I'm running that takes a while, so I want to keep track of how far it's through, by printing to the console. So something like: import sys def myMethod(): i = 0 while (i<1000000): i = i+1 output_str = str(i) + "\n" sys.stdout.write(output_str) # same as print sys.stdout.flush() myMethod() How can I have this print while it's running, rather than

How can I print to console while the program is running in python? [duplicate]

混江龙づ霸主 提交于 2019-12-18 15:51:01
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: How to flush output of Python print? I have have an algorithm that I'm running that takes a while, so I want to keep track of how far it's through, by printing to the console. So something like: import sys def myMethod(): i = 0 while (i<1000000): i = i+1 output_str = str(i) + "\n" sys.stdout.write(output_str) # same as print sys.stdout.flush() myMethod() How can I have this print while it's running, rather than

How can I redirect R warning messages to STDOUT?

泪湿孤枕 提交于 2019-12-18 15:19:24
问题 I'm using a grid engine to run R scripts. The STDERR is taken seriously under this setup, so I would like to keep it clean and have only real/serious/fatal errors printed to STDERR. The problem is my R script generate various STDERR messages which are not really serious warnings... for example, scan seems to print to STDERR the number of items it read. Can I redirect (from within R) STDERR to STDOUT? 回答1: Look at the help page for sink() : ‘sink’ diverts R output to a connection. If ‘file’ is

How can I redirect R warning messages to STDOUT?

落爺英雄遲暮 提交于 2019-12-18 15:19:16
问题 I'm using a grid engine to run R scripts. The STDERR is taken seriously under this setup, so I would like to keep it clean and have only real/serious/fatal errors printed to STDERR. The problem is my R script generate various STDERR messages which are not really serious warnings... for example, scan seems to print to STDERR the number of items it read. Can I redirect (from within R) STDERR to STDOUT? 回答1: Look at the help page for sink() : ‘sink’ diverts R output to a connection. If ‘file’ is

How to close stdout and stderr in C?

非 Y 不嫁゛ 提交于 2019-12-18 13:11:40
问题 I need to close stdout and stderr for one of my C program. How is it possible without exiting the program in execution? 回答1: What have you tried? Doesn't fclose work? 回答2: You can just: fclose(stdout); fclose(stderr); For anybody wondering why you might want to do this, this is a fairly common task for a daemon/service process on Unix. However you should be aware that closing a file descriptor may have unintended consequences: When you open new files these now free descriptors will be used.

Writing a pytest function for checking the output on console (stdout)

你离开我真会死。 提交于 2019-12-18 12:52:38
问题 This link gives a description how to use pytest for capturing console outputs. I tried on this following simple code, but I get error import sys import pytest def f(name): print "hello "+ name def test_add(capsys): f("Tom") out,err=capsys.readouterr() assert out=="hello Tom" test_add(sys.stdout) Output: python test_pytest.py hello Tom Traceback (most recent call last): File "test_pytest.py", line 12, in <module> test_add(sys.stdout) File "test_pytest.py", line 8, in test_add out,err=capsys

How to execute a command and get return code stdout and stderr of command in C++

随声附和 提交于 2019-12-18 12:36:23
问题 Given the following answer (first c++11 answer): How to execute a command and get output of command within C++ using POSIX? Here is the implementation for your convinience: #include <cstdio> #include <iostream> #include <memory> #include <stdexcept> #include <string> #include <array> std::string exec(const char* cmd) { std::array<char, 128> buffer; std::string result; std::shared_ptr<FILE> pipe(popen(cmd, "r"), pclose); if (!pipe) throw std::runtime_error("popen() failed!"); while (!feof(pipe

Opening a TStream on stdin/stdout in a Delphi console app

扶醉桌前 提交于 2019-12-18 10:09:29
问题 I'm trying to write a Delphi console application that creates a TStream for its standard input, and another TStream for its standard output. (It will be launched by a host app with its input and output redirected to pipes, and will be passing binary data to/from that host app, so TStream will be much better-suited to the task than ReadLn/WriteLn.) How do I go about opening a TStream on standard input or standard output? 回答1: Off the top of my head: InputStream := THandleStream.Create