簡體   English   中英

如何將shell輸出實現為C語言的輸入?

[英]How to implement shell output as a input of C language?

例如,猜猜我有一個已編譯的c程序,名為“ binaryOutput”。 在Unix環境中,[root @ blablabla〜] ./binaryOutput打印出如下結果:[0] [1] [0] [1] [1]

我想將這些結果用作另一個C文件的輸入。

在C語言中,我可以運行文件。

system("./binaryOutput") ;

在代碼之后,我想將數字添加為數組的輸入。 我該怎么做?

popen示例。 您可以獲取命令的輸出。

#include <stdio.h>

int main(void)
{
  FILE *fp=NULL;
  char line[1024];
  if ( (fp=popen("ls", "r"))==NULL )
  {
    return -1;
  }
  while( fgets(line, 1023, fp)!=NULL )
  {
    printf("read from popen:%s", line);
  }
  pclose(fp);
  return 0;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM