我正在为我的大学进行作业。我有一个关于如何显示特定目录内包含的所有文件的问题。我的工作环境是LINUX UBUNTU 14.04 G ++编译器。通过在LINUX中使用C++在特定目录中包含显示文件
让我们举个例子,我想显示/输出全部在这个目录里的文件
/home/user/Desktop/TEST/FileSystem
File contains inside FOLDER FileSystem
-test.txt
-abc.txt
-item.txt
-records.txt
我不知道是否可以做到使用:
- 使用执行系统命令,通过调用标准库头。
#include <iostream>
#include <stdlib.h>
int main()
{
system("pwd"); // Directory: /home/user/Desktop/TEST/FileSystem
system("ls"); // Display every files contain in the FileSystem Folder
}
输出我的预期:
/FileSystem Folder contains:
-test.txt
-abc.txt
-item.txt
-records.txt
我如何可以编写我的源代码,使我能够实现这个输出/显示我的预期。我通过使用Google搜索来浏览一些互联网来源。但是我发现理解它有困难。这就是为什么我决定在这里发布我的问题。
非常感谢您提前帮助我解决我的编码问题。
阅读[Boost Filesystem library](http://www.boost.org/doc/libs/1_56_0/libs/filesystem/doc/index.htm)。 – 2014-09-05 10:57:30
指出,谢谢。 :) @JoachimPileborg – J4X 2014-09-05 11:01:35
Boost文件系统或Qt与它的QDir类。 当你午餐system()命令时,你应该以某种方式捕获输出。看看这个线程http://stackoverflow.com/questions/478898/how-to-execute-a-command-and-get-output-of-command-within-c – madduci 2014-09-05 11:23:26