1

Ive had a look at this post: Find if string ends with another string in C++

I am trying to achieve a similar goal.

Basically i want to take a file list from a directory and filter out any files which do not end with a specified allowed extention for processing in my program.

In java this would be performed by creating a method and passing the extention accross as a string then using .endswith in the following statement. C++ does not appear to support this so how would i go about it?

   for (int fileList = 0; fileList < files.length; fileList++)
   {
      //output only jpg files, file list is still full
      if(files[fileList].toString().endsWith(extension))
      {
          images.add(files[fileList]);
      }//end if
   }//end for

Thanks in advance

Community
  • 1
  • 1
Chriss
  • 11
  • 1

3 Answers3

2
bool endsWith(std::string const & s, std::string const & e) {
    if (s.size() < e.size())
        return false;
    return s.substr(s.size() - e.size()) == e;
}
Erik
  • 88,732
  • 13
  • 198
  • 189
  • will that not just check if the extention is the same length. I need to exclude based on the actual extention ie .pdf or .doc only – Chriss Apr 07 '11 at 14:28
  • note - `e` needs to contain the ".", or an attempt to match on "ml" files will also match "html". – AShelly Apr 07 '11 at 14:29
  • @chriss, no the last line is comparing the last `e.size()` characters in the string to `e`. – AShelly Apr 07 '11 at 14:30
1

If using boost::filesystem is ok for you then you could try

#include <boost/filesystem.hpp>
//...

boost::filesystem::path dir_path ("c:\\dir\\subdir\\data");
std::string extension(".jpg");
for (boost::filesystem::directory_iterator it_file(dir_path);
     it_file != boost::filesystem::directory_iterator();
     ++it_file)
{
  if ( boost::filesystem::is_regular_file(*it_file) &&
       boost::filesystem::extension(*it_file) == extension)
  {
    // do your stuff
  }
}

This will parse the given directory path and you then just have to filter desired extension.t

MSalters
  • 173,980
  • 10
  • 155
  • 350
marchelbling
  • 1,909
  • 15
  • 23
0

Next example checks if the filename ends with the jpg extension :

#include <iostream>
#include <string>
using namespace std;


bool EndsWithExtension (const string& str,const string &extension)
{
    size_t found = str.find_last_of(".");
    if ( string::npos != found )
    {
        return (extension == str.substr(found+1) );
    }

    return false;
}

int main ()
{
  string filename1 ("c:\\windows\\winhelp.exe");
  string filename2 ("c:\\windows\\other.jpg");
  string filename3 ("c:\\windows\\winhelp.");

  cout << boolalpha << EndsWithExtension(filename1,"jpg") << endl;
  cout << boolalpha << EndsWithExtension(filename2,"jpg") << endl;
  cout << boolalpha << EndsWithExtension(filename3,"jpg") << endl;
}
BЈовић
  • 62,405
  • 41
  • 173
  • 273
  • ok. Thank you very much. Just to clarify so i understand how this is working; the method looks in the passed string for the "." and if it finds it then looks at the remaining characters "found+1" after the "." to see if there is a match for "expectedExtentsion" – Chriss Apr 07 '11 at 14:39
  • @Chriss Right. It finds the position of the dot. If the dot is found, it assumes the rest is the file extension. If the substring from the dot position matches the expected extension, it returns true.Otherwise false – BЈовић Apr 07 '11 at 14:41
  • ok. thank you very much for your explanation this has been a great help! – Chriss Apr 07 '11 at 14:56