编写具有以下功能的程序或函数:
Hello, world!写入标准输出流。(对于这一挑战,没有任何其他形式的输出是可以接受的,因为关注的重点是I/O,而不是程序本身的琐碎行为。)取决于它是否成功:Hello, world!,程序/函数就会退出,而不会有任何进一步的行为。Error writing "Hello, world!"写入标准错误流。(就此挑战而言,您不需要对错误处理本身进行错误处理。)stdout是一个封闭的文件句柄,不存在文件描述符1,或者那些情况转换成您正在使用的语言和操作系统);必须至少将下列情况视为成功(即不是错误):
- Standard output connects to a terminal, and `Hello, world!` is displayed onscreen.
- Standard output connects to a file, and `Hello, world!` is written into the file.您可以选择什么是输出错误的细节,只要它符合上述规则。
下面是一种在Linux上使用bash模拟上述每一种错误条件的方法(您不必使用Linux,但这可能是最简单的测试系统):
your_program_here >&- # nonexistent stdout
your_program_here > /dev/full # out of disk space
mkfifo test # note: change "test" to a filename that isn't in use
true < test &
your_program_here > test # connecting to a program that doesn't want input
rm test # clean up the FIFO we used earlier前两个测试案例是确定性的。最后一个不是(它依赖于竞争条件);为了测试目的,我建议在程序启动和实际输出之间添加一个延迟到标准输出,以确保以公开错误的方式解决竞争条件。
这是一个密码-高尔夫挑战,所以更短更好。和(几乎)总是一样,我们用字节来测量程序的长度。
发布于 2017-04-12 05:41:50
发布于 2017-04-12 18:55:08
f(a){a="Error writing \"Hello, world!\"";write(1,a+15,13)-13&&write(2,a,29);}应召
main(){f(1); return 0;}https://codegolf.stackexchange.com/questions/116207
复制相似问题