繁体   English   中英

C编程linux,读取ping或ls -l等系统输入

[英]C Programming linux , read system inputs like ping or ls -l

我尝试制作一个自定义方法,导致返回一个带有系统输出的字符。

像这样的伪代码。

char *my_Out(char *in ){
    in = system ("ping %s",in);
    return in;
}

谢谢您的帮助。

您可以使用popen ,它会返回一个可以读取输出的流。 通过读取直到文件结尾,变成一个字符串(可能是根据需要动态增长的字符串),您可以实现您所要求的内容。

一些东西

  1. system()不是printf样式函数。 您之前需要使用sprintf()创建您的参数。
  2. system()的返回值是int,非char
  3. 覆盖函数参数通常不是一个好主意。

你想做什么? 看起来所有这个函数都运行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.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM