LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-02-2005, 01:03 PM   #1
bendeco13
Member
 
Registered: Oct 2004
Distribution: Fedora 7
Posts: 232

Rep: Reputation: 30
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
 
Old 11-02-2005, 01:11 PM   #2
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
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

Last edited by jtshaw; 11-02-2005 at 01:38 PM.
 
Old 11-02-2005, 01:14 PM   #3
narc
Member
 
Registered: Aug 2004
Location: Montréal
Distribution: Linux from scratch
Posts: 68

Rep: Reputation: 15
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.
 
Old 11-02-2005, 01:33 PM   #4
bendeco13
Member
 
Registered: Oct 2004
Distribution: Fedora 7
Posts: 232

Original Poster
Rep: Reputation: 30
Thank you all this will surely solve my problem. I appreciate yalls replies.
Bendeco
 
Old 06-11-2007, 07:44 AM   #5
vishalbtrivedi
LQ Newbie
 
Registered: Jun 2007
Posts: 1

Rep: Reputation: 0
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;
 
Old 06-11-2007, 09:01 PM   #6
sidney.harrell
Member
 
Registered: Apr 2006
Location: Stafford, VA
Distribution: Ubuntu 6.06, 7.04, Slackware 11
Posts: 45

Rep: Reputation: 15
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

Last edited by sidney.harrell; 06-11-2007 at 09:39 PM.
 
Old 10-07-2008, 09:40 AM   #7
launic
LQ Newbie
 
Registered: Oct 2008
Posts: 1

Rep: Reputation: 0
how would I go about making that function recursive so that it'll open subdirectories and get those files too??
 
Old 10-07-2008, 09:26 PM   #8
narc
Member
 
Registered: Aug 2004
Location: Montréal
Distribution: Linux from scratch
Posts: 68

Rep: Reputation: 15
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.

Last edited by narc; 10-07-2008 at 09:28 PM.
 
Old 11-02-2010, 12:08 PM   #9
transmogrifox
LQ Newbie
 
Registered: Jan 2009
Posts: 12

Rep: Reputation: 1
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
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Apache Directory Listing Of NFS Mount, cannot view directory list via apache luqmana Linux - Networking 2 12-19-2005 06:03 AM
how to list how many files in a directory? malaka56 Linux - Software 8 09-02-2005 05:37 AM
How to list all the files in a directory Linh Programming 2 05-11-2004 10:09 AM
Missing List.* files in URPMI directory Ronw Linux - General 2 10-08-2003 03:57 AM
list only files in current directory xscousr Programming 8 09-22-2003 07:35 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:21 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration