0

I am trying to create a program that randomly chooses a folder given a base path, then within the new folder randomly chooses a video to be opened and start playing.

My main issue is finding the number of files within a path given. Is there any function that can do something like that? Or similar? What kind of headers do I need? etc..

The random part is kind of easy. After fixing that issue I would like to know if I am able to launch a video for example while executing the program, which should the last step of my program.

I have searched a lot before posting that, I know you might think its already out there, but I was not able to find something specific enough for what I want.

I hope you can help me.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
RyanLK
  • 13
  • 1

3 Answers3

2

You should look into boost.filesystem. C++ without boost (or another library set, like Qt) has very limited capabilities.

There is an example in doc:

int main(int argc, char* argv[])
{
  path p (argv[1]);   // p reads clearer than argv[1] in the following code

  try
  {
    if (exists(p))    // does p actually exist?
    {
      if (is_regular_file(p))        // is p a regular file?   
        cout << p << " size is " << file_size(p) << '\n';

      else if (is_directory(p))      // is p a directory?
      {
        cout << p << " is a directory containing:\n";

        copy(directory_iterator(p), directory_iterator(), // directory_iterator::value_type
          ostream_iterator<directory_entry>(cout, "\n")); // is directory_entry, which is
                                                          // converted to a path by the
                                                          // path stream inserter
      }

      else
        cout << p << " exists, but is neither a regular file nor a directory\n";
    }
    else
      cout << p << " does not exist\n";
  }

  catch (const filesystem_error& ex)
  {
    cout << ex.what() << '\n';
  }

  return 0;
}

Of course you can use the directory_iterator inside "for" loop:

#include <boost/filesystem.hpp>
#include <boost/range/iterator_range.hpp>
#include <iostream>

using namespace boost::filesystem;

int main(int argc, char *argv[])
{
    path p(argc > 1? argv[1] : ".");

    if(is_directory(p)) {
        std::cout << p << " is a directory containing:\n";

        for(auto& entry : boost::make_iterator_range(directory_iterator(p), {}))
            std::cout << entry << "\n";
    }
}
peper0
  • 3,111
  • 23
  • 35
0

You obviously need to modify this function to make it work for you. But this is a function that I was able to find and make. I think it requires windows.h. What it does is that it adds the filenames of all the files inside Bin/Pictures to a vector called mTextureNames.

 void Editor::LoadTextureFileNames()
    {   
        string folder = "../Bin/Pictures/";
        char search_path[200];
        sprintf(search_path, "%s*.*", folder.c_str());
        WIN32_FIND_DATA fd; 
        HANDLE hFind = ::FindFirstFile(search_path, &fd); 
        if(hFind != INVALID_HANDLE_VALUE) { 
            do { 
                // read all (real) files in current folder
                // , delete '!' read other 2 default folder . and ..
                if(! (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ) {
                    this->mTextureNames.push_back(fd.cFileName);
                }
            }while(::FindNextFile(hFind, &fd)); 
            ::FindClose(hFind); 
        } 
    }
Alex
  • 365
  • 4
  • 17
0

My main issue is finding the number of files within a path given.

Though the function glob called is C, it shouldn't be a problem if your C++ compiler is compatible with C. You can always wrap it in C++ :) Man glob describes how to use it. The GLOB_ONLYDIR lets you limit the results to directories.

The easiest way to play the videos is by calling system() and execute the video in your favorite player

Community
  • 1
  • 1
jcoppens
  • 5,306
  • 6
  • 27
  • 47