show whats in directory!

This is a discussion on show whats in directory! within the C++ Programming forums, part of the General Programming Boards category; so this is sample of showing what's in directory. i believe it is with classes...could i remake it, so it ...

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

    show whats in directory!

    so this is sample of showing what's in directory.
    i believe it is with classes...could i remake it, so it would be without classes? because it is abit too hard for me. could you give me a hand redoing it? thank you.

    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
    Mar 2009
    Posts
    399
    It's certainly not going to be easier to do it without std::string and std::vector, that's for sure. If you want to do it using only C types, you'll have to manage all the memory yourself and it's going to add a lot of unneeded complexity to the program.

  3. #3
    Grey Wizard C_Sparky's Avatar
    Join Date
    Sep 2009
    Posts
    50
    Here's an alternative Win32 API version:
    Code:
    #include <windows.h>
    #include <iostream>
    using namespace std;
    
    int main()
    {
        HANDLE hFind;
        WIN32_FIND_DATA FindData;
        int ErrorCode;
        BOOL Continue = TRUE;
    
        cout << "A decent FindFirst/Next demo." << endl << endl;
    
        hFind = FindFirstFile("C:\\Directory\\*.*", &FindData);
    
        if(hFind == INVALID_HANDLE_VALUE)
        {
            ErrorCode = GetLastError();
            if (ErrorCode == ERROR_FILE_NOT_FOUND)
            {
                cout << "There are no files matching that path/mask\n" << endl;
            }
            else
            {
                cout << "FindFirstFile() returned error code " << ErrorCode << endl;
            }
            Continue = FALSE;
        }
        else
        {
            cout << FindData.cFileName << endl;
        }
    
        if (Continue)
        {
            while (FindNextFile(hFind, &FindData))
            {
                cout << FindData.cFileName << endl;
            }
    
            ErrorCode = GetLastError();
    
            if (ErrorCode == ERROR_NO_MORE_FILES)
            {
                cout << endl << "All files logged." << endl;
            }
            else
            {
                cout << "FindNextFile() returned error code " << ErrorCode << endl;
            }
    
            if (!FindClose(hFind))
            {
                ErrorCode = GetLastError();
                cout << "FindClose() returned error code " << ErrorCode << endl;
            }
        }
    
        return 0;
    }

  4. #4
    Registered User
    Join Date
    Nov 2009
    Posts
    14
    c_sparky, thank you! could you help me figure some things out?
    so i deleted most of the things, so i could understand the code better :P but the main thingy is still working. How could i add all there files and folders which are 'FindData.cFileName' to a massive? i tried to add them to like char massive, but it aint working for me... and then i have to go through all of them to pick only the ones that have 'male', 'female' or both inside the name, so i used switch. so first of all can i use CHAR to add the names in the massive and later use them??
    Code:
    #include <cstdlib>
    #include <windows.h>
    #include <iostream>
    #include <string.h>
    #include <stdio.h>
    using namespace std;
    
    int main()
    {
        HANDLE hFind;
        WIN32_FIND_DATA FindData;
        int ErrorCode;
        char path[40], nr;
        
        
        cout << "Enter a path"<<endl<< "egz: c:\\folder\\*.*" << endl;
        cin>>path;
        cout<< "Content inside:"<<endl<<endl;
        
        hFind = FindFirstFile(path, &FindData);
    
        if(hFind != INVALID_HANDLE_VALUE)
        {
            cout << FindData.cFileName << endl;
            
         while (FindNextFile(hFind, &FindData))
           { 
               cout << FindData.cFileName << endl;
           }
    
            ErrorCode = GetLastError();
    
            if (ErrorCode == ERROR_NO_MORE_FILES)
            {
                cout << endl << "All files logged.\n" << endl;
            }
            system("PAUSE");
        }
        cout<< "1-female, 2-male, 3-both, +-off\n";
        while ((nr=getchar())!='+')
        {
              switch (nr)
              {
                     case '1': 
                          break;
                     case '2':
                          break;
                     case '3':
                          break;
                     default:
                             break;
              } 
        }
        return 0;
    }

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,648
    Quote Originally Posted by apisio
    How could i add all there files and folders which are 'FindData.cFileName' to a massive?
    What is a "massive"?

    Listing the entries of a directory requires the use of non-standard facilities at this point of time. What you first posted is likely written for a *nix system, though MinGW or Cygwin could be used to get it to run on Windows. C_Sparky's example is very much Windows specific. What are your requirements?

    If you happen to have Boost available, I would suggest Boost.Filesystem as it is cross platform, and progress has been made such that it may eventually be included in modified form in the C++ standard library.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Nov 2009
    Posts
    14
    hello there, laserlight. hmm...im not sure how it is called...like int a[10] <- that how do u call this a[10] ? is it array ? :P sry m8, i'm foreign.
    hmm, i have to work with console application only. and the programme i use is DEVc++... i'm just begginer and this is my first task at college.. do you have any suggestions, my friend?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,648
    Quote Originally Posted by apisio
    hmm...im not sure how it is called...like int a[10] <- that how do u call this a[10] ? is it array ? :P sry m8, i'm foreign
    Ah, yes, array would be the word
    In your original example the std::vector<std::string> is actually the dynamic array of directory entries.

    Quote Originally Posted by apisio
    hmm, i have to work with console application only. and the programme i use is DEVc++... i'm just begginer and this is my first task at college.. do you have any suggestions, my friend?
    Whether your program is a console application or not does not matter in this case. Dev-C++ is an IDE, not a compiler, but its default compiler is the MinGW port of g++, which should be able to compile both your original example and C_Sparky's example, assuming that they are correct. Given that this is a homework assignment, I doubt you will be able to use Boost.

    So, which to pick? If you have learnt about the Windows API, or if it is at least in your syllabus, then you could try to work with C_Sparky's example. If not, what is good about your original example is its use of the standard library: so there should, by right, be less new things to learn since you should know (or at least begin to know) the standard library. You can then concentrate on what is specific to <sys/types.h> and <dirent.h> (and if you do have to learn about std::vector and std::string, at least the knowledge would be more immediately reusable).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Not able to access directory of home folder
    By Bargi in forum Linux Programming
    Replies: 1
    Last Post: 02-06-2008, 01:45 AM
  2. Finding files in a directory
    By smarta_982002 in forum C Programming
    Replies: 1
    Last Post: 01-25-2008, 09:10 AM
  3. Building a tree from a directory structure
    By geekoftheweek in forum C Programming
    Replies: 2
    Last Post: 11-26-2007, 02:15 AM
  4. Browsers that show a directory
    By sean in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 03-02-2004, 12:30 PM
  5. open directory and copy files
    By 5n4k3 in forum C++ Programming
    Replies: 3
    Last Post: 08-06-2003, 09:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21