Jump to content




Recent Status Updates

  • Photo
      30 Sep
    rhossis

    laptop hard disk seated beneath motherboard but with no access panel. 7 hours to replace :(

    Show comments (3)
  • Photo
      19 Sep
    Chall

    I love it when you go to write a help thread, then while writing, you reach an enlightenment, and figure it out yourself.

    Show comments (3)
View All Updates

Developed by Kemal Taskin
Photo
- - - - -

List files in a directory for C++


  • Please log in to reply
4 replies to this topic

#1 Groogy

Groogy

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 179 posts

Posted 01 August 2010 - 04:14 PM

Hi!

I need a way to list all files in a directory and I would prefer if there were a common way to do it in both Linux and Windows? If not then give me the two different ways and I'll just make it work with both Linux and Windows using the preprocessor defines.

Links for further information is also appreciated.
Thanks on beforehand.
  • 0
My Code Blog - My Github - Ascension Project - Madness Script Project - Simple-Garbage-Collector Project
There is bound to be something useful somewhere.

#2 Flying Dutchman

Flying Dutchman

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1,068 posts
  • Location:::1
  • Programming Language:C++, Python

Posted 01 August 2010 - 04:47 PM

#include <iostream>
#include <dirent.h>

using namespace std;

int main() {

    DIR*     dir;
    dirent*  pdir;

    dir = opendir(".");     // open current directory

    while (pdir = readdir(dir)) {
        cout << pdir->d_name << endl;
    }
    closedir(dir);
    return 0;
}

Some further reading: cplusplus.com linuxquestions.org wikipedia.org

I think it works on *nix as well.
  • 1

The roots of education are bitter, but the fruit is sweet.


#3 Groogy

Groogy

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 179 posts

Posted 01 August 2010 - 06:23 PM

I found a Windows clone of the dirent.h file thanks to you on the Wikipedia page you wrote.

Thanks & +rep!
  • 0
My Code Blog - My Github - Ascension Project - Madness Script Project - Simple-Garbage-Collector Project
There is bound to be something useful somewhere.

#4 Guest

Guest

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 914 posts
  • Location:USA
  • Programming Language:C, Java, Python, Bash, Others
  • Learning:Assembly, Scheme

Posted 01 August 2010 - 08:25 PM

I may be a little late, but I have another resource that you may find useful:
C Directory Tutorial [intma.in]
  • 0
Root Beer == System Administrator's Beer
Download the new operating system programming kit! (some assembly required)

#5 Ancient Dragon

Ancient Dragon

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 378 posts

Posted 01 August 2010 - 08:57 PM

For MS-Windows see the FindFirstFile() and FindNextFile() win32 api functions. I think there boost also has functions that are os independent.
  • 0
Visit Grandpa's Forums, a social networking forum, with family-oriented arcade games, blogs, discussion forums, and photo albums.