2015-08-14 43 views
0

我不肯定還是關於這個問題的措辭,反正 -捕捉「標準輸入重定向的文件位置字符串」在C

我已經產生了一些值作爲命令行參數,也可執行需要一個文件從標準是這樣的:

[email protected]:~$./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); 
} 

這可能嗎?

回答

1

號Shell解釋你的命令行,看見一個<打開文件進行讀取,做了fork(2)和子進程,替換stdin使用dup2(2)之前實際執行程序此文件句柄。

相關問題