stdout

How to print stdout immediately?

我的梦境 提交于 2019-12-10 01:07:09
问题 How can I immediately output stdout ? stdout is going to print after all input is complete. require 'open3' def run(cmd) Open3.popen3(cmd) do |stdin, stdout, stderr, thread| Thread.new do stdout.each {|l| puts l} end Thread.new do while thread.alive? stdin.puts $stdin.gets end end thread.join end end run ("ruby file_to_test.rb") file_to_test.rb: puts "please, enter s" puts "please, enter q" s = gets.chomp! q = gets.chomp! puts s puts q The result after running main.rb is: somestring

how to print directly to a text file in both python 2.x and 3.x?

萝らか妹 提交于 2019-12-10 01:04:31
问题 Instead of using write() , what are the other way to write to a text file in Python 2 and 3? file = open('filename.txt', 'w') file.write('some text') 回答1: You can use the print_function future import to get the print() behaviour from python3 in python2: from __future__ import print_function with open('filename', 'w') as f: print('some text', file=f) If you do not want that function to append a linebreak at the end, add the end='' keyword argument to the print() call. However, consider using f

How can I do an atomic write to stdout in python?

折月煮酒 提交于 2019-12-09 16:48:26
问题 I've read in some sources that the print command is not thread-safe and the workaround is to use sys.stdout.write command instead, but still it doesn't work for me and the writing to the STDOUT isn't atomic. Here's a short example (called this file parallelExperiment.py): import os import sys from multiprocessing import Pool def output(msg): msg = '%s%s' % (msg, os.linesep) sys.stdout.write(msg) def func(input): output(u'pid:%d got input \"%s\"' % (os.getpid(), str(input))) def

How do you use Log4j to write/capture stdout and stderr to a file and using Windows and Tomcat 5.5 (Java)?

不打扰是莪最后的温柔 提交于 2019-12-09 16:11:55
问题 I am using Windows 2008 R2 and Apache Tomcat 5.5, for your information. STDOUT and STDERR can be automatically logged through Apache Tomcat properties, via Logging tab -> Redirect Stdout and Redirect Stderror textboxes. But I want to control this through log4j. I'm trying to leverage ConsoleAppender and the TimeAndSizeRollingAppender class to rollover what would normally be controlled by Apache Tomcat's innate logging. Basically, however Tomcat redirects stdout and stderr to a file, I want to

Why does wget output to stderr rather than stdout?

非 Y 不嫁゛ 提交于 2019-12-09 15:07:56
问题 After 30mins of futile attempt to capture the output of wget , I figured out that the program writes to stderr rather than the stdout . Searching in web and stack-overflow reveals this to be a well-known fact. Any idea why is this so? 回答1: It's well known, because it's in the manual. Reporting messages on stderr is common, because messages are separated from regular output on stdout . This is useful when you combine several tools with a pipe. In this case it would be bad, when regular output

erasing terminal output on linux

孤街醉人 提交于 2019-12-09 10:55:25
问题 I was writing a command line program which will have a status bar, much like wget. The main problem I'm facing is: how do I delete what I've already sent into stdout/stderr? I had on idea: use the backspace char '\b' and erase the output I've sent. Is that the best way? Is it the only way? Is there a better way? PS: I don't want to use anything like ncurses. Plain old C please. Thanks EDIT: Can I also go up and/or down? Example: I have 10 lines of output, I want to change the 3rd line from

Python getstatusoutput replacement not returning full output

Deadly 提交于 2019-12-09 03:52:40
问题 I came across this great replacment for the getstatusoutput() function in Python 2.* which works equally well on Unix and Windows. However, I think there is something wrong with the way the output is constructed. It only returns the last line of the output, but I can't figure out why. Any help would be awesome. def getstatusoutput(cmd): """Return (status, output) of executing cmd in a shell.""" """This new implementation should work on all platforms.""" import subprocess pipe = subprocess

fork() and printf()

谁都会走 提交于 2019-12-08 20:56:29
As I understood fork() creates a child process by copying the image of the parent process. My question is about how do child and parent processes share the stdout stream? Can printf() function of one process be interrupted by other or not? Which may cause the mixed output. Or is the printf() function output atomic? For example: The first case: parent: printf("Hello"); child: printf("World\n"); Console has: HeWollorld The second case: parent: printf("Hello"); child: printf("World\n"); Console has: HelolWorld printf() is not guaranteed to be atomic. If you need atomicity, use write() with a

CMake not redirecting stderr with execute_process

拈花ヽ惹草 提交于 2019-12-08 18:07:29
I'm trying to redirect stdout and stderr to the same file using CMake. I'm using the execute_process option in CMake with the ERROR_FILE and OUTPUT_FILE option specified. I'm successfully capturing the output, but the error is not there. What am I doing wrong? File CMakeLists.txt add_test(NAME test${ID} COMMAND ${CMAKE_COMMAND} -DEXE=../examples/test${exampleID} -DID=${ID} -DARGS=${args} -P ${CMAKE_CURRENT_SOURCE_DIR}/Tester.cmake ) File Tester.cmake separate_arguments( ARGS ) # Run the test execute_process( COMMAND "${EXE}" ${ARGS} ERROR_FILE test${ID}.out OUTPUT_FILE test${ID}.out )

What is “standard input”?

谁说胖子不能爱 提交于 2019-12-08 17:17:26
问题 I was tasked with creating a test program in C that reads the contents of the standard input and then prints them. But I have a little doubt: what is exactly standard input ? Is it what I type in the keyboard? Is it a file I have to read? Both of them? And the same goes for standard output : is it the console? a file? 回答1: The C standard (e.g. C99 or C11) defines what should be expected from the standard <stdio.h> header (after having suitably #include -d it). See stdio(3) man page. Then you