I want list all the files with the same substring as a name?
for example there are 10 files with same substring as "testoutput" I want list all of them. How can I do that?
You do have two logical subtasks:
1. Get a list of filenames from the filesystem.
2. Select from list entries that contain the substring. string::contains()
Or you will need to use your o/s file systems calls directly.
- For Linux you have to use the POSIX opendir, readdir, closedir which allow you to get all files. It is then up to you to filter the results based on your substring.
- With Win32 it's a little different to what keskiverto says: the FindFirstFile call (corresponding to opendir) can be used with wild card, so you should be able to get the filtered set of files directly. The calls required are FindFirstFile, FindNextFile, FindClose
(There are also corresponding Microsoft CRT calls -- _findfirst, _findnext, and _findclose -- but I've never seen them used.)
If we're talking about cross-platform GUI frameworks that provide directory enumeration, then we've also got wxWidgets (wxDir::GetFirst, wxDir::GetNext, wxDir::Traverse) as well as Qt.
(Of course MFC provides one, too; but it's just a class wrapper for the Win32 calls. And it's not cross-platform.)
Would it be better to use is_directory() rather than exists() on line 25 ?
It could be, depending on the functionality you want. I would rather have an exception thrown if it's fed the name of a file, but I'm good with it just returning an empty list if the directory doesn't exist. Definitely something that should be noted in documentation, though.