cross platform directory listing

I googled for ways to get a directory listing, and from the results i saw, it looked like it was going to be a pain in the butt in c++. Looked like you cannot do it for one code for any OS? So what is confusing me, is if this section of code is suppose to be for linux, why is it working in windows too? I tried it in linux and it gave a directory listing, i tried it in my windows virtual box and it also gave a directory listing? So is dirent.h file standard for windows now too? IF so it now safe to assume that systems will have dirent.h file?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <dirent.h>

#ifdef WIN32
    bool islinux = false;
    
#else
    bool islinux = true;
#endif




int getdir (std::string dir, std::vector<std::string> &files)
{
    DIR *dp;
    struct dirent *dirp;
    if((dp  = opendir(dir.c_str())) == NULL) {
        std::cout << "Error(" << errno << ") opening " << dir << std::endl;
        return errno;
    }

    while ((dirp = readdir(dp)) != NULL) {
        files.push_back(std::string(dirp->d_name));
    }
    closedir(dp);
    return 0;
}

int main(){
    std::string dir = std::string(".");
    std::vector<std::string> files = std::vector<std::string>();

    getdir(dir,files);

    for (unsigned int i = 0;i < files.size();i++) {
        std::cout << files[i] << std::endl;
    }
}




linux:
1
2
3
4
5
6
7
8
9
metulburr@ubuntu:~$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.7/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.7.3-1ubuntu1' --with-bugurl=file:///usr/share/doc/gcc-4.7/README.Bugs --enable-languages=c,c++,go,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.7 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.7 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --enable-plugin --with-system-zlib --enable-objc-gc --with-cloog --enable-cloog-backend=ppl --disable-cloog-version-check --disable-ppl-version-check --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.7.3 (Ubuntu/Linaro 4.7.3-1ubuntu1) 
metulburr@ubuntu:~$ 


and in windows I'm using:
TDM-GCC 4.7.1 32-bit Release


EDIT: oh yeah aside from the boost method
Last edited on
I think TDM-GCC/MinGW comes with a version of dirent.h for windows. If you compiled your code in Visual C++ it would probably not have worked.
EDIT: oh yeah aside from the boost method

what's wrong with the boost method?
nothing wrong with the boost method. I was looking for the most portable method without using a 3rd party library if at all possible. I steer clear of Microsoft software anyways so Visual C++ is not applicable to me.
nothing wrong with the boost method. I was looking for the most portable method without using a 3rd party library if at all possible.

It isn't.

You need to choose between the Boost approach (which is very prob going to end up as part of C++14) or one of the platform specific solutions (POSIX or Windows, with MinGW providing ports of the POSIX calls.)

Andy
Last edited on
You need to choose between the Boost approach (which is very prob going to end up as part of C++14)

hmm interesting. I thought since it was from 1998-2011 since last "update", there wouldnt be one till 2022 or something. I wasnt aware of one upcoming in 2014
interesting, never saw that before, thanks for the link cire
For info about C++14 dev status for Clang, GCC, and Visual C++, see:

C++98, C++11, and C++14 Support in Clang
http://clang.llvm.org/cxx_status.html

C++1y/C++14 Support in GCC
http://gcc.gnu.org/projects/cxx1y.html

C++11/14 STL Features, Fixes, And Breaking Changes In VS 2013
http://blogs.msdn.com/b/vcblog/archive/2013/06/28/c-11-14-stl-features-fixes-and-breaking-changes-in-vs-2013.aspx

Andy
It isn't.

You need to choose between the Boost approach (which is very prob going to end up as part of C++14) or one of the platform specific solutions (POSIX or Windows, with MinGW providing ports of the POSIX calls.)

Andy



I dont know this thing you are talking is there any article that can explain what boosting and 3rd party library is??
closed account (Dy7SLyTq)
article that can explain what boosting and 3rd party library is??


not boosting. boost. its a c++ api that is like the swiss army knife of libraries. it has almost everything you need but it is very big
http://www.boost.org

third party libraries are libraries/apis you download that help you program in a certain way easier. these include sdl, sfml, opengl, boost, allegro
Thanks a lot that helped me :)
Last edited on
Topic archived. No new replies allowed.