Thread: little help here please..

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    14

    little help here please..

    Hello there. i need to do this thingy... i have to write a code with which i could enter a path to directory and it would show every folder and file in that directory on the screen. also i need to add theese names into a massive... i got this code, but this is a bit too hard for me, is it possible to make it simple? if not, then could you explain few things in it? please..
    for example this 'std::string const & path
    , std::vector <std::string> & dirEntryNames' ??

    the code i have for list of files in directory:
    Code:
    #include <sys/types.h>        
    #include <dirent.h>           
    #include <errno.h>
    #include <vector>             
    #include <string>             
    #include <iostream>           
    
    int dirfunct
    ( std::string const & path
    , std::vector <std::string> & dirEntryNames )
    {
           DIR * dir = opendir( path.c_str() );
           if ( dir != NULL )
           {
                   dirent * entry( NULL );
                   do
                   {
                           entry = readdir( dir );
                           if ( entry != NULL )
                           {
                                   dirEntryNames.push_back( entry->d_name );
                           }
                   }
                   while ( entry!= NULL );
    
                   if ( closedir( dir ) )
                   {
                       return -1;
                   }
    
           }
           else 
           {
                   return -1;
           }
           return 0;
    }
    
    int main()
    {
            std::string path;
           std::cout << "Path: ";
           std::cin >> path;
           std::vector<std::string> entries;
           if ( !dirfunct(path, entries) )
           {
                   std::cout << "Contents of path:\n";
                   for ( int i=0; i < entries.size(); ++i )
                   {
                           std::cout << entries[i] << "\n";
                   }
           }
    }

  2. #2
    Registered User
    Join Date
    Nov 2009
    Posts
    14
    btw, i believe it is written with classes. how could i make it withouth classes??

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The program is written in C++, and will not run at all in C.

    C has no classes.

    If you want to compile something in C, then set your compiler options to use C, and have your code files end with dot c, instead of dot cpp.

    Your compiler should have a setting under "options" to set this up.

    Post up your start with this, and we'll work from there. Tell us what's not working.

Popular pages Recent additions subscribe to a feed