我試圖做一個自定義的方法是什麼原因返回一個字符與系統輸出。C編程linux,讀取系統輸入如ping或ls -l
這樣的僞代碼。
char *my_Out(char *in){
in = system ("ping %s",in);
return in;
}
感謝您的幫助。
我試圖做一個自定義的方法是什麼原因返回一個字符與系統輸出。C編程linux,讀取系統輸入如ping或ls -l
這樣的僞代碼。
char *my_Out(char *in){
in = system ("ping %s",in);
return in;
}
感謝您的幫助。
您可以使用popen
,它會返回一個可讀取輸出的流。通過閱讀直到文件結束,轉換成一個字符串(可能是一個動態增長的字符串),你可以實現你所要求的。
有幾件事情
system()
不是一個printf風格的函數。您需要先使用sprintf()
來創建您的參數。system()
的返回值是一個int,非char你想做什麼?它看起來像所有這個功能是運行ping
(沒有-c參數,將永遠不會在linux上完成運行)。
我不同意第3點。雖然它可能很不尋常,並且打破了你的風格,但它沒有任何問題。 – 2010-03-04 01:39:43
Duplicate the stdout to some other file descriptor by using dup2.After the execution of the command read all the lines from the file using that file descriptor and return it.
如果你包含''(它聲明'system()'),你就會知道它返回一個整數而不是'char *'。 –
2010-03-04 03:00:21