2017-05-27 58 views
0

我試圖在使用execlp函數的Unix環境中編寫C程序。我正在執行tsort命令(tsort獲取文本文件作爲輸入)。錯誤地使用execlp函數

void syserr(char * str) 
{ 
    perror(str); 
    exit(1); 
} 
int main() 
{ 
    int inpfd; 
    int pipeC[2]; 
    char buffer[4]; 
    execlp("tsort","tsort","t.txt"); 
    syserr("execlp "); 
} 

的錯誤是:

tsort: extra operand 'AWA\211\377AVI\211\366AUI\211\325ATL\215%\350\a ' 
Try 'tsort --help' for more information. 

我做了什麼錯?

回答

3

引述the manual on execlp

[...]的參數列表必須由NULL指針終止,並且,因爲這些是可變參數的功能,該指針必須轉換(字符*)NULL

你沒有這樣做。試試:

execlp("tsort","tsort","t.txt",(char*)NULL);