stdin

How to read the standard input into string variable until EOF in C?

拥有回忆 提交于 2019-11-26 20:03:03
问题 I am getting "Bus Error" trying to read stdin into a char* variable. I just want to read whole stuff coming over stdin and put it first into a variable, then continue working on the variable. My Code is as follows: char* content; char* c; while( scanf( "%c", c)) { strcat( content, c); } fprintf( stdout, "Size: %d", strlen( content)); But somehow I always get "Bus error" returned by calling cat test.txt | myapp , where myapp is the compiled code above. My question is how do i read stdin until

How do I check if stdin has some data?

本小妞迷上赌 提交于 2019-11-26 18:48:58
In Python, how do you check if sys.stdin has data or not? I found that os.isatty(0) can not only check if stdin is connected to a TTY device, but also if there is data available. But if someone uses code such as sys.stdin = cStringIO.StringIO("ddd") and after that uses os.isatty(0) , it still returns True. What do I need to do to check if stdin has data? On Unix systems you can do the following: import sys import select if select.select([sys.stdin,],[],[],0.0)[0]: print "Have data!" else: print "No data" On Windows the select module may only be used with sockets though so you'd need to use an

How to read from a file or stdin in Bash?

主宰稳场 提交于 2019-11-26 18:03:26
In Perl the following code will read from file specified on command line args or from stdin: while (<>) { print($_); } This is very convenient. I just want to know what's the simplest way to read from file or stdin in bash. Fritz G. Mehner The following solution reads from a file if the script is called with a file name as the first parameter $1 otherwise from standard input. while read line do echo "$line" done < "${1:-/dev/stdin}" The substitution ${1:-...} takes $1 if defined otherwise the file name of the standard input of the own process is used. Perhaps the simplest solution is to

How to write data to existing process's STDIN from external process?

别来无恙 提交于 2019-11-26 17:42:50
问题 I'm seeking for ways to write data to the existing process's STDIN from external processes, and found similar question How do you stream data into the STDIN of a program from different local/remote processes in Python? in stackoverlow. In that thread, @Michael says that we can get file descriptors of existing process in path like below, and permitted to write data into them on Linux. /proc/$PID/fd/ So, I've created a simple script listed below to test writing data to the script's STDIN (and

How to read terminal's input buffer immediately after keypress

主宰稳场 提交于 2019-11-26 17:19:12
问题 I want to read arrow keypresses in my c program and replace them(immediately in the terminal itself) by some other string. I am trying to implement the bash history functionality as in unix terminals. I wrote this code. int main(int argc, char *argv[]) { char c; char str[1024]; int i = 0; while((c = fgetc(stdin)) != '\n'){ if(((int)c) == 27){ c=fgetc(stdin); c=fgetc(stdin); if (c == 'A') { printf("%c[A%c[2K",27, 27); printf("UP"); } } str[i++] = c; } printf("\n"); return 0; } But, this doesn

How to clear stdin before getting new input?

孤者浪人 提交于 2019-11-26 17:00:43
问题 I have read about 5-10 different advices how to clear stdin, but none of them suits my needs. The thing is that fflush(stdin) worked perfectly at my computer, but unfortunately it doesn't seem to work everywhere, so I need something with the same functionality. Every other way I tried clears stdin when it is not empty but requires user input when stdin IS empty, which means it requires input in a moment I dont want to get any (+ it discards it anyway). The question is: Can I somehow make sure

Redirect stdin and stdout in Java

蓝咒 提交于 2019-11-26 16:38:42
问题 I'm trying to redirect stdin and stdout of a subprocess in java, eventually i'm going to have the output go to a JTextArea or something. Here is my current code, Process cmd = Runtime.getRuntime().exec("cmd.exe"); cmd.getOutputStream().write("echo Hello World".getBytes()); cmd.getOutputStream().flush(); byte[] buffer = new byte[1024]; cmd.getInputStream().read(buffer); String s = new String(buffer); System.out.println(s); The output looks like this: Microsoft Windows [Version 6.1.7601]

setvbuf not able to make stdin unbuffered

走远了吗. 提交于 2019-11-26 16:33:31
My main intention was to make getchar return as soon as it gets a character instead of waiting for the ENTER key. I tried this int main() { setvbuf(stdin,NULL,_IONBF,0); getchar(); return 0; } Comparing this with the prototype of setvbuf setvbuf ( FILE * stream, char * buffer, int mode, size_t size ); it should set stdin to unbuffered mode. But still getchar() keeps waiting for ENTER I've seen related posts like this Printing while reading characters in C which are suggesting alternate methods to make stdin unbuffered. But I am curious to know as to why setvbuf method does not work Jonathan

java: how to both read and write to & from process thru pipe (stdin/stdout)

邮差的信 提交于 2019-11-26 16:20:39
问题 (i'm new to java) I need to start a process and receive 2 or 3 handles: for STDIN, STDOUT, (and STDERR), so I can write input to the process and receive its output, the same way command line pipes behave (e.g. "grep") in Python this is acheived with the following code: from subprocess import Popen, PIPE p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE) (child_stdin, child_stdout) = (p.stdin, p.stdout) child_stdin.write('Yoram Opposum\n') child_stdin.flush() child_stdout.readlines() What's

Continuously read from STDOUT of external process in Ruby

 ̄綄美尐妖づ 提交于 2019-11-26 15:37:13
I want to run blender from the command line through a ruby script, which will then process the output given by blender line by line to update a progress bar in a GUI. It's not really important that blender is the external process whose stdout I need to read. I can't seem to be able to catch the progress messages blender normally prints to the shell when the blender process is still running, and I've tried a few ways. I always seem to access the stdout of blender after blender has quit, not while it's still running. Here's an example of a failed attempt. It does get and print the first 25 lines