Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
有没有办法从 c++ 运行 linux 命令 ls,并在 c++ 中将所有输出存储在一个数组中?
谢谢
如果您坚持实际运行ls,您可以使用popen启动进程并读取输出:
ls
popen
FILE *proc = popen("/bin/ls -al","r"); char buf[1024]; while ( !feof(proc) && fgets(buf,sizeof(buf),proc) ) { printf("Line read: %s",buf); }
但是您最好自己阅读目录内容和文件信息,使用opendirand readdir。
opendir
readdir