我试图做一个自定义的方法是什么原因返回一个字符与系统输出。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