简体   繁体   English

如何在c ++中将ls命令输出到数组

[英]how to get output of ls command to an array in c++

Is there a way to run the linux command ls, from c++, and get all the outputs stored in one array, in c++? 有没有办法从c ++运行linux命令ls,并在c ++中获取存储在一个数组中的所有输出?

Thanks 谢谢

If you insist on actually running ls , you can use popen to launch the process and read the output: 如果你坚持实际运行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);
}

But you could probably better be reading the directory contents and file info yourself, using opendir and readdir . 但你可能最好使用opendirreaddir自己阅读目录内容和文件信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM