2014-12-02 88 views
1

例如在文件打印標準錯誤消息:在文件寫入標準錯誤消息 命令是如何使用EXECL()函數在C

command > /dev/null 2>text.file" 

所以,

execl("gcc" , "gcc" , "-g" , "test.c" , ">" , "/dev/null 2" , ">" , "test" , NULL); 

EXECL是返回-1 。它不是打印文本中的錯誤。文件

+4

重定向是* shell *功能,'exec'不會調用shell。嘗試'system',而是調用一個shell。 – 2014-12-02 13:26:45

+0

或者通過告訴exec運行shell來手動運行你的命令。 – 2014-12-02 13:35:39

+0

please,探索更多@JoachimPileborg – user3391465 2014-12-02 13:36:50

回答

0

流重定向功能由您的shell提供,並不是執行命令的標準方式。

你可以做的是啓動一個shell(比如bash),然後你可以通過傳遞你的命令來重定向stderr。

execl("/bin/bash", "bash", "-c", "gcc -g test.c > /dev/null 2>test", NULL); 
+0

Bash期望第一個非選項參數是它將執行的shell腳本。使用'-c'選項並將所有內容作爲單個字符串傳遞(這基本上是'system'的功能)。 – 2014-12-02 14:08:26

+0

現在execl正在返回-1。 – user3391465 2014-12-02 14:11:42

+0

@JoachimPileborg它正在與系統..謝謝 – user3391465 2014-12-02 14:21:34

0

試試這個:

#include<unistd.h> 
#include<fcntl.h> 
//Remaining code till here 
dup2(open("/dev/null",O_WRONLY), 1); //redirect stdout to /dev/null 
dup2(open("text.file",O_WRONLY), 2); //redirect stderr to text.file 
execl("gcc" , "gcc" , "-g" , "test.c", NULL); 

注意:您可能需要添加O_CREAT標誌text.file。