一个简单的C++程序,Test函数用来测试调用Linux的系统命令ls -l
#include<cstdlib> #include<string> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; const int N = 300; void Test(void){ char line[N]; FILE *fp; string cmd = "ls -l"; // system call const char *sysCommand = cmd.data(); if ((fp = popen(sysCommand, "r")) == NULL) { cout << "error" << endl; return; } while (fgets(line, sizeof(line)-1, fp) != NULL){ cout << line << endl; } pclose(fp); } int main(){ Test(); return 0; }
时间: 2024-10-25 18:24:40