Jump to content

List files in a directory for C++

- - - - -

  • Please log in to reply
4 replies to this topic

#1
Groogy

Groogy

    Programmer

  • Members
  • PipPipPipPip
  • 183 posts
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.
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

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1

#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.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#3
Groogy

Groogy

    Programmer

  • Members
  • PipPipPipPip
  • 183 posts
I found a Windows clone of the dirent.h file thanks to you on the Wikipedia page you wrote.

Thanks & +rep!
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

    Writes binary right handed and hex left handed

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

#5
Ancient Dragon

Ancient Dragon

    Programming Expert

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




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users