How to get all filename in a given directory
I want to get the file name in a given path is there any apis available . My programming environment开发者_C百科 is vc++ mfc
You should look at FindFirstFile and FindNextFile, or MFC's wrapper for them, CFileFind.
Boost has a great platform independent filesystem library. It'll work with MFC.
Here's an example from their reference:
#include <iostream>
#include <filesystem>
using std::tr2::sys;
using std::cout;
int main(int argc, char* argv[])
{
std::string p(argc <= 1 ? "." : argv[1]);
if (is_directory(p))
{
for (directory_iterator itr(p); itr!=directory_iterator(); ++itr)
{
cout << itr->path().filename() << ' '; // display filename only
if (is_regular_file(itr->status())) cout << " [" << file_size(itr->path()) << ']';
cout << '\n';
}
}
else cout << (exists(p) ? "Found: " : "Not found: ") << p << '\n';
return 0;
}
You can also use MFC: CFileFind
There is also CListBox::Dir. It's very convenient if you want to fill a list box with file names.
精彩评论