C++ List Files In Directory
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 |
Something like this:
Code:
#include <sys/types.h> Key functions opendir readdir closedir |
Your could start reading the specific chapters on the GNU C library regarding the File System Interface
at http://www.gnu.org/software/libc/man...stem-Interface 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. ;-) narc. |
Thank you all this will surely solve my problem. I appreciate yalls replies.
Bendeco |
Query in Code
#include <sys/types.h>
#include <dirent.h> #include <errno.h> #include <vector> #include <string> #include <iostream> using namespace std; /*function... might want it in some class?*/ int getdir (string dir, vector<string> &files) { DIR *dp; struct dirent *dirp; if((dp = opendir(dir.c_str())) == NULL) { cout << "Error(" << errno << ") opening " << dir << endl; return errno; } while ((dirp = readdir(dp)) != NULL) { files.push_back(string(dirp->d_name)); } closedir(dp); return 0; } int main() { string dir = string("."); vector<string> files = vector<string>(); getdir(dir,files); for (unsigned int i = 0;i < files.size();i++) { cout << files[i] << endl; } return 0; } Can you please explain below mentioned two lines in Code ??? DIR *dp; struct dirent *dirp; |
Thank You, narc, so much for the link:
http://www.gnu.org/software/libc/man...stem-Interface I followed it to the top: http://www.gnu.org/software/libc/man...index.html#Top And it was exactly what I've been looking for! Here's the main page for all gnu docs: http://www.gnu.org/manual/manual.html You know, I've never really done site specific searches, but this made me check out google's advanced search page. I restricted it to gnu.org, searched for vectors and got back 29,000 results. Maybee trying to reinvent the wheel just makes you appreciate one made by a professional. http://www.gnu.org/software/plotutil...plotutils.html |
how would I go about making that function recursive so that it'll open subdirectories and get those files too??
|
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) 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 |
All times are GMT -5. The time now is 03:41 AM. |