c - C程序中Linux系统调用的输出

标签 c linux unix system

我想在我的 C 程序中操作 ls -al 命令的输出。 有办法吗?

int main()
{
    int return_value;
    return_value = system("ls -al ");
    return return_value;
}

最佳答案

您需要一个流程的管道。

#include <stdio.h>
#include <stdlib.h>

int main()
{ 

  FILE *fp;
  char output[1024];

  fp = popen("/bin/ls -al", "r");
  if (fp == NULL)
    exit(1);

  while (fgets(output, 1023, fp) != NULL) 
    printf("%s", output);

  pclose(fp);

  return 0;
}

关于c - C程序中Linux系统调用的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31670975/

相关文章:

mysql - 在centos 7上安装mysql时出错

c++ - 如何使用套接字 c/C++ 以 block 的形式发送文件?

unix - 使用 awk 删除列

linux - Bash 脚本在目录中保留 'x' 个字符?

c - x86 Assembly Force 高速缓存存储

c - 空字符串的 strcmp

c - 用于输入的 fgets()/sscanf 在功能上运行良好。当 fnct 离开并返回时,stdin 中是否有额外的输入/行?

c - 解释这段代码的正确方法是什么?

linux - 使用 'import' 拍摄的黑色屏幕截图 (ImageMagick)

消费者生产者代码未声明的错误linux调试