【发布时间】:2015-11-19 15:22:21
【问题描述】:
我写到标准输入,它出现在屏幕上(0个标准输入,1个标准输出,2个标准错误
const int SIZE=12;
int main()
{
int fd = open("input.txt", O_RDWR);
char buffer[SIZE] = "Hello world";
write(fd, buffer, SIZE - 1);
lseek(fd, 0, SEEK_SET);
char mem[SIZE];
read(fd, mem, SIZE - 1);
mem[SIZE] = '\0';
write(0, mem, SIZE - 1);
printf("\n");
write(1, mem, SIZE - 1);
printf("\n");
write(2, mem, SIZE - 1);
printf("\n");
return 0;
}
输出:
Hello world
Hello world
Hello world
这是如何工作的?标准输入不连接键盘吗?
谢谢。
【问题讨论】:
-
一旦你有未定义的行为,无论发生什么,无论多么难以置信,都是允许的。
标签: c++ operating-system file-descriptor system-calls redirectstandardoutput