[关闭]
@guochy2012 2014-01-17T15:38:50.000000Z 字数 846 阅读 1082

APUE练习

主要用来练习Linux下的C编程

  1. /*
  2. ============================================================================
  3. Name : Process003.c
  4. Author : Chunyang Guo
  5. Version :
  6. Copyright : Your copyright notice
  7. Description : Hello World in C, Ansi-style
  8. ============================================================================
  9. */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <unistd.h>
  13. #include <string.h>
  14. //popen与system的不同之处就是它采用管道进行工作
  15. //打开后就像普通文件一样进行操作
  16. int main(void) {
  17. puts("!!!Hello World!!!"); /* prints !!!Hello World!!! */
  18. // system("ps aux");
  19. // sleep(10);
  20. FILE *read_fp;
  21. char buffer[BUFSIZ + 1];
  22. int chars_read;
  23. memset(buffer, 0x00, sizeof(buffer));
  24. read_fp = popen("ps aux", "r");
  25. if (read_fp == NULL){
  26. exit(EXIT_FAILURE);
  27. }
  28. //chars_read = fread(buffer, sizeof(char), BUFSIZ, read_fp);
  29. while((chars_read = fread(buffer, sizeof(char), BUFSIZ, read_fp) ) > 0){
  30. buffer[chars_read - 1] = 0;
  31. printf("%s", buffer);
  32. }
  33. pclose(read_fp);
  34. return EXIT_SUCCESS;
  35. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注