In python, if you want to open all the files that started with "apl_" you can simply open "apl*". How do you do something similar in C++.
Say I have a file which I know starts with "llll", but have unknown suffixes. How do I open it?
In python, if you want to open all the files that started with "apl_" you can simply open "apl*". How do you do something similar in C++.
Say I have a file which I know starts with "llll", but have unknown suffixes. How do I open it?
You cannot just 'simply open "apl*"' in Python. If you try this:
open("apl*")
You will get an IOError—unless you happen to have a file literally named "apl*".
There are two basic ways to do this in Python, both of which can be translated to C++.
First:
[open(f) for f in glob.glob("apl*")]
On most non-Windows platforms, glob.glob
translates directly into the POSIX function glob
. Unfortunately, on Windows, it's called _glob
, doesn't exist on all versions of Windows, and has severe limitations. So, instead, you want to use FindFirstFile and FindNextFile. The examples given on the linked pages should show you how to do it.
Second:
[open(f) for f in os.listdir(".") if f.startswith("apl")]
You can again do this by just using FindFirstFile/FindNextFile to iterate the entire directory, and then filter on whether data->cFileName starts with "apl", but you might as well use much cleaner (and more portable) boost::filesystem::directory_iterator.