ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am sort of new to c++ (I took a c class in college, but it only delt with numbers), but I'm very familiar with python. I'm trying to write a script/header file that takes a given directory and returns the contents of the directory in an array. In python there is a function called os.listdir('directory'). I'm looking to write or use something similar to this.
Anyone have any ideas of where I should start looking?
Thanks in advance
Bendeco
Opening and reading directories are achieved by the use of opendir and readdir functions located in the direct.h header.
Lots of reading for your C/C++ pleasure. ;-)
Wow, I first answered that thread 3 years ago. ;-)
I would think, out of my head, that if you get the content of a given directory, you can identify if each file is a directory or a normal file. You pass a pointer to (or the reference of) a container (a list, a map, even a stack) while cat'ting the path of the directories or file and accumulating file names into it.
Code:
void GetDirContent(current_dir, ptr_to_container)
{
list = GetListofFiles(current_dir)
for each member of the list
{
new_name = concatenate member to current_dir (with an extra slash /)
if member is a directory
{
GetDirContent(new_name, ptr_to_container);
}
else
{
ptr_to_container->Add(new_name);
}
}
}
Once the recursion stops, it unrolls back with a container filled with file names.
Don't forget to ignore . and .. directories - which readdir() usually returns.
This is an old thread, but I thought I would add my 2 cents for anybody who stumbles upon it like I did.
For a recursive directory search, the first thing that comes to mind for me is, "what common Linux program does this?":
cp -R ...
download the source code from the cp function, study the source, and learn how it has been done in one of the most-used command line functions in UNIX :-) My thinking is it has got to be a very robust bit of code that will teach you good habits. http://www.linuxquestions.org/questi...ommand-142046/
Or you can download the source from your specific distribution.
Cheers
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.