【发布时间】:2011-01-23 11:50:41
【问题描述】:
我尝试制作一个自定义方法,导致返回带有系统输出的字符。
这样的伪代码。
char *my_Out(char *in ){
in = system ("ping %s",in);
return in;
}
感谢您的帮助。
【问题讨论】:
-
如果你包含了
<stdlib.h>(声明system()),你就会知道它返回一个整数而不是char *。
我尝试制作一个自定义方法,导致返回带有系统输出的字符。
这样的伪代码。
char *my_Out(char *in ){
in = system ("ping %s",in);
return in;
}
感谢您的帮助。
【问题讨论】:
<stdlib.h>(声明system()),你就会知道它返回一个整数而不是char *。
您可以使用popen,它会返回一个流,您可以从中读取输出。通过读取直到文件结束,进入一个字符串(可能是一个根据需要动态增长的字符串),您可以实现您所要求的。
【讨论】:
ls -l 中的信息之一,例如,您可以使用库函数 (stat) 更轻松地获得它。
stat 通常是系统调用,而不是库函数。 :-P
stat 通常是一个库函数包装一个系统调用(他靠着傻瓜生活......)
struct stat 布局不同,并且需要保持用户代码二进制兼容)。最初,不,这是一个直接的系统调用。尽管如此,好电话。 +1
一些事情
system() 不是 printf 风格的函数。您需要先使用sprintf() 来创建您的论点。system()的返回值是一个int,不是一个char你想做什么?看起来这个函数所做的只是运行ping(如果没有 -c 参数,它永远不会在 linux 上完成运行)。
【讨论】:
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.
【讨论】: