I wonder how to check If a file exist or not :
For Example I have many files :
Boba.txt
James.txt
Jamy.txt
Boby.txt
How can I check if the file starting with Bob exists ?
Answer
Note, I'm assuming that you're on a Windows system and that the files are in the same directory.
You can use the FindFirstFile and FindNextFile functions to iterate through a directory. The prefix can be included in search term.
Example
std::string strSearch = "C:\\Some Directory\\Bob*";
WIN32_FIND_DATAA ffd;
HANDLE hFind = FindFirstFileA(strSearch .c_str(), &ffd);
do
{
std::string strFile = ffd.cFileName;
}
while (FindNextFileA(hFind, &ffd) != 0);
Proper error checking and how to deal with directories is left as an exercise for the reader.
No comments:
Post a Comment