0

I want to crop just only jpg files in a directory.

Tried if statement, but don't know what should inside if().

void Cutimage::load_images(string img_prefix)
{
    for (const auto& entry : fs::directory_iterator(img_prefix)) {
        cout << entry.path() << endl; //output all directory's filenames
        if () { //I applied some statements but doesn't work
            string path_string = entry.path().string();
            crop_images(path_string);
        }
    }
rsjaffe
  • 5,600
  • 7
  • 27
  • 39
Teewip
  • 41
  • 1
  • 8
  • If you have a string, how do you check if the string ends with ".jpg", or perhaps ".jpeg"? If you can answer this question, setting aside the issue of filesystems, then you should be able to figure out what to do here. This has nothing to do with filesystems, or jpg files, but a simple string manipulation. – Sam Varshavchik Feb 18 '19 at 03:15
  • Possible duplicate of https://stackoverflow.com/questions/37197850/scan-directory-and-subdirectories-for-txt-files – Galik Feb 18 '19 at 03:32

1 Answers1

0

No, the filesystem utilities of the standard library do not have such feature. However, it is fairly simple to iterate all directory entries, and conditionally perform an operation if the file name ends in ".jpg", which appears to be what you've attempted.

Tried if statement, but don't know what should inside if().

The directory entry has an member function that returns the full path of the directory. The path class has an member function that returns a string. The string class has a member function that returns true if the string ends with the sub string given as an operand. This appears to be what you're looking for.

eerorika
  • 232,697
  • 12
  • 197
  • 326