![](/img/trans.png)
[英]What’s the best way to check if a file exists in C++? (cross platform)
[英]Best way to check if file matching abc* exists in a particular directory
我想知道如何檢查/tmp/
路徑中是否存在與core*
匹配的文件。 實際文件可以如下所示。
corexyz.txt
coreabc.tar.gz
core*
如果知道完整的文件名,我可以使用stat
或access
來了解文件是否存在。 但是在這里,我想知道是否有任何文件匹配core*
。
bool checkFile (std::string ABC)
{
return true; /* return true if file ABC* exists */
}
如果你可以使用 c++17文件系統庫對你來說會很有趣。
您可以使用std::filesystem::directory_iterator遍歷目錄中的所有文件,並使用path.filename()檢查它們是否以給定前綴開頭。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.