【问题标题】:capture the "stdin redirected file location string" in c [duplicate]在c中捕获“stdin重定向文件位置字符串” [重复]
【发布时间】:2015-11-08 06:43:05
【问题描述】:

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

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

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); 
}

有可能吗?

【问题讨论】:

    标签: c command-line-arguments command-prompt stdin


    【解决方案1】:

    没有。 shell 解释你的命令行,看到&lt; 打开文件进行读取,执行fork(2) 并在子进程中,用这个文件句柄替换stdin 使用dup2(2) 实际执行之前你的程序。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-14
      • 1970-01-01
      • 2011-05-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多