LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   C++ List Files In Directory (https://www.linuxquestions.org/questions/programming-9/c-list-files-in-directory-379323/)

bendeco13 11-02-2005 01:03 PM

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

jtshaw 11-02-2005 01:11 PM

Something like this:
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;
}

It isn't the most robust thing.. but it works... modify/use as you wish.

Key functions
opendir
readdir
closedir

narc 11-02-2005 01:14 PM

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.

bendeco13 11-02-2005 01:33 PM

Thank you all this will surely solve my problem. I appreciate yalls replies.
Bendeco

vishalbtrivedi 06-11-2007 07:44 AM

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;

sidney.harrell 06-11-2007 09:01 PM

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

launic 10-07-2008 09:40 AM

how would I go about making that function recursive so that it'll open subdirectories and get those files too??

narc 10-07-2008 09:26 PM

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.

transmogrifox 11-02-2010 12:08 PM

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.