Friday, 4 May 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

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...