通过在LINUX中使用C ++,显示文件包含在特定目录中 [英] Display files contain inside a particular directory by using C++ in LINUX
问题描述
我正在为我的大学工作。我有一个问题,如何显示特定目录中包含的所有文件。我的工作环境是在LINUX UBUNTU 14.04 G ++编译器。
I'm working on assignment for my university. I have a question on how to display all the files contain inside a particular directory. My working environment is on LINUX UBUNTU 14.04 G++ Compiler.
让我们举一个例子,我想显示/输出这个DIRECTORY
Let's take an example, I want to display/output all the files inside this DIRECTORY
/home/user/Desktop/TEST/FileSystem
File contains inside FOLDER FileSystem
-test.txt
-abc.txt
-item.txt
-records.txt
我不确定是否可以使用:
I'm not sure whether it can be done by using:
- 使用执行系统命令,通过调用标准库头。
-Using Execute System Command, by calling standard library header.
#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
}
/ p>
OUTPUT that I expected:
/FileSystem Folder contains:
-test.txt
-abc.txt
-item.txt
-records.txt
如何编写我的源代码所以我能够实现这个OUTPUT /显示,我的预期。我通过一些互联网来源通过谷歌搜索。但我发现很难理解它。这就是为什么我已经决定在这里发布我的问题。
How can I code my source code so that I'm able to achieving this OUTPUT/Display that I expected. I have go through some internet sources by googling it. But I find out difficulty on understand it. That's why I have made a decision to post my question on here.
感谢您提前帮助我解决我的编码问题。
Thank You in advance to you guys for helping me to solve my coding problem.
推荐答案
您需要首先打开需要列出文件的目录,然后您需要读取目录。
You need to first open directory for which you need to list files after that you need to read directory.
添加 #include 用于使用apis。
Add #include for using apis.
#include <dirent.h>
/* open the directory "/home/" for reading. */
DIR* dir = opendir("/home/users");
entry = readdir(dir)); //files or directories in /home
//Add logic to verify entry is file or directory
请参阅此主题 http://www.cpp-home.com/ tutorials / 107_6.htm
这篇关于通过在LINUX中使用C ++,显示文件包含在特定目录中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!