在 c 中捕获 "stdin redirected file location string"

capture the "stdin redirected file location string" in c

无论如何,我仍然不确定问题中的措辞 --

我有一个可执行文件,它将一些值作为命令行参数,并且还从标准输入中获取一个文件,如下所示:

user@pc:~$./executable 0.12345 0.12345 < some/directory/somefile.txt

现在有什么方法可以在代码中获取文件位置 some/directory/somefile.txt 吗?

我能做的是修改代码以将文件位置作为另一个命令行参数,但在那种情况下我需要更改很多东西。

所以,如果我能做这样的事情就更好了--

int main(int argc, char **argv)
{
    char cmd_prompt[80];
    char file_location[80];
    grab_command_prompt(cmd_prompt);
    split_and_grab_last_token(cmd_prompt, file_location);
    /* this line below should print: "some/directory/somefile.txt" */
    printf("%s\n", file_location); 
}

可能吗?

没有。 shell 解释您的命令行,看到 < 打开文件进行读取,在 实际执行您的程序之前执行 fork(2) and in the child process, replaces stdin with this filehandle using dup2(2)