-
-
Save kosta/9777932 to your computer and use it in GitHub Desktop.
import java.io.IOException; | |
/** | |
* Class that copies stdin to stdout, as compained about as not being cleanly | |
* writable in Java on Hacker News. | |
* In real code, you would just write IOUtils.copy(System.in, System.out), | |
* which does basically the same thing. | |
* This does not catch any exceptions as a) this is just an "exercise" and | |
* b) all we could do with them is pretty-print them. So let the runtime | |
* print them for you. | |
* | |
* Usage: javac inout.java && echo -e "foo\nbar" | java InOut | |
*/ | |
class InOut { | |
public static void main(String[] args) throws IOException { | |
byte[] buffer = new byte[8192]; | |
while(true) { | |
int bytesRead = System.in.read(buffer); | |
if (bytesRead == -1) { | |
return; | |
} | |
System.out.write(buffer, 0, bytesRead); | |
} | |
} | |
} |
jamesmoriarty
commented
Mar 26, 2014
C#
using System;
public class Program
{
public static void Main()
{
while(true)
{
int input = Console.In.Read();
if(input == -1) break;
Console.Out.Write((char)input);
}
}
}
@ssijak I think you misunderstood what that Java program does. It copies the input verbatim to the output.
$ printf 'hello\nworld\n' | python3 -c 'print(input())'
hello
$
It also mangles binary data since it uses print() which will add a newline to the end of its arguments by default (try end=''
to never write a final newline).
$ printf 'binarydata' | python3 -c 'print(input())' | hexdump -C
00000000 62 69 6e 61 72 79 64 61 74 61 0a |binarydata.|
0000000b
instead of...
$ printf 'binarydata' | hexdump -C
00000000 62 69 6e 61 72 79 64 61 74 61 |binarydata|
0000000a
$
Observe there is no newline in the last example.
@17twenty I don't think your program respects binary data either because the print statement needs a final comma to suppress the final newline.
@rShetty Your example appears to only copy the first line in Ruby. Does puts
add a final newline as well?
Anything that "gets a line" uses a newline as a sentinel value and is not a solution. The sentinel of an input stream is an EOF marker (in python the sentinel is an empty string returned from the .read()
method). I think Programming Pearls has a section about the importance of an unique sentinel value.
Python
Using partial
as suggested in Transforming Code into Beautiful Idiomatic Python (the example slide):
from functools import partial
from sys import stdout, stdin
for block in iter(partial(stdin.read, 32), ''):
stdout.write(block)
Note that partial
is optional — lambda: stdin.read(32)
also works.
/In C/
include <stdio.h>
int main(int argc, char **argv)
{
int c;
while((c = getchar()) != EOF)
putchar(c);
return 0;
}
in bash:
echo
In French:
Copiez entrée à la sortie
In Go:
io.Copy(os.Stdout, os.Stdin)
Libraryless:
b := make([]byte, 1024)
for n, err := os.Stdin.Read(b); err == nil; n, err = os.Stdin.Read(b) {
os.Stdout.Write(b[:n])
}
In Turkish:
Girdiyi çıktıya aktar.
uhh I dont understand why this isnt a valid solution
import java.util.Scanner;
public class Main
{
public static void main (String[]args)
{
while(true)
{
Scanner sc = new Scanner(System.in);
System.out.println(sc.nextLine());
}
}
}