我对这个用于教育目的的小代码有疑问。我无法理解它是如何工作的。
#include <stdio.h>
#include <fcntl.h>
#define FNAME "info.txt"
#define STDIN 0
int main(){
int fd;
fd = open(FNAME, O_RDONLY);
close(STDIN); //entry 0 on FDT is now free
dup(fd); //fd duplicate is now stored at entry 0
execlp("more","more",0);
}
通过启动这个程序,它会在终端上打印文件“info.txt”的内容。我不明白为什么!“更多”和 STDIN(键盘或文件)之间的链接在哪里?
为什么如果我在没有 args 且文件上没有重定向的情况下使用更多,它只会显示一个帮助屏幕,但重定向它使用文件作为输入?