Reading from stdin

You can do something like this to read 10 bytes: char buffer[10]; read(STDIN_FILENO, buffer, 10); remember read() doesn’t add ‘\0′ to terminate to make it string (just gives raw buffer). To read 1 byte at a time: char ch; while(read(STDIN_FILENO, &ch, 1) > 0) { //do stuff } and don’t forget to #include <unistd.h>, STDIN_FILENO … Read more

Scanf/Printf double variable C

For variable argument functions like printf and scanf, the arguments are promoted, for example, any smaller integer types are promoted to int, float is promoted to double. scanf takes parameters of pointers, so the promotion rule takes no effect. It must use %f for float* and %lf for double*. printf will never see a float … Read more

Read expression for grep from standard input

Use -f with a single dash to denote the standard input: $ echo Content | grep -f – notice.html <meta http-equiv=”Content-Type” content=”text/html; charset=ISO-8859-1″> … Note: This has been tested with GNU grep – I am not sure if it’s specified by POSIX.

Interactive input/output using Python

None of the current answers worked for me. At the end, I’ve got this working: import subprocess def start(executable_file): return subprocess.Popen( executable_file, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) def read(process): return process.stdout.readline().decode(“utf-8″).strip() def write(process, message): process.stdin.write(f”{message.strip()}\n”.encode(“utf-8”)) process.stdin.flush() def terminate(process): process.stdin.close() process.terminate() process.wait(timeout=0.2) process = start(“./dummy.py”) write(process, “hello dummy”) print(read(process)) terminate(process) Tested with this dummy.py script: #!/usr/bin/env python3.6 import … Read more

How to extract tar archive from stdin?

Use – as the input file: cat largefile.tgz.aa largefile.tgz.ab | tar zxf – Make sure you cat them in the same order they were split. If you’re using zsh you can use the multios feature and avoid invoking cat: < largefile.tgz.aa < largefile.tgz.ab tar zxf – Or if they are in alphabetical order: <largefile.tgz.* | … Read more

how to redirect STDOUT to a file in PHP?

It is possible to write STDOUT directly to a file in PHP, which is much easier and more straightforward than using output bufferering. Do this in the very beginning of your script: fclose(STDIN); fclose(STDOUT); fclose(STDERR); $STDIN = fopen(‘/dev/null’, ‘r’); $STDOUT = fopen(‘application.log’, ‘wb’); $STDERR = fopen(‘error.log’, ‘wb’); Why at the very beginning you may ask? … Read more

What’s the fastest way to read from System.in in Java?

Is there any faster way of doing this in Java? Yes. Scanner is fairly slow (at least according to my experience). If you don’t need to validate the input, I suggest you just wrap the stream in a BufferedInputStream and use something like String.split / Integer.parseInt. A small comparison: Reading 17 megabytes (4233600 numbers) using … Read more