Friday, May 4, 2018

c++ - find file starting with a certain string




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

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...