Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How can I get the list of png files in a directory in Linux? I saw boost library and a few. But how can I do this without using any third party libraries? Please help with a sample code.
Posted
Comments
[no name] 31-Jul-15 9:03am    
http://www.bing.com/search?q=c%2b%2b+list+of+files+ubuntu
Sergey Alexandrovich Kryukov 31-Jul-15 9:31am    
But C++ or C++/CLI? Do you agree to consider CLR as not a 3rd-party library? :-)
Anyway, not using CLR BCL would be a ridiculous thought, and using it for this purpose — way too trivial.
—SA

 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 31-Jul-15 9:32am    
He-he, 5ed. And the similar answer could be given for C++/CLI; please see my comment to the question.
—SA
C++
#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;
}
</string></string></string></iostream></string></vector></errno.h></dirent.h>
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900