C++ stat判断路径是文件还是目录

C++ stat判断路径是文件还是目录

 1 #include <iostream>
 2 #include <sys/stat.h>
 3 
 4 using namespace std;
 5 
 6 void foo ( const char* path ) {
 7     struct stat s;
 8     if ( stat ( path, &s ) == 0 ) {
 9         if ( s.st_mode & S_IFDIR ) {
10             cout << "DIR" << endl;
11         } else if ( s.st_mode & S_IFREG ) {
12             cout << "FILE" << endl;
13         } else {
14             cout << "?" << endl;
15         }
16     } else {
17         cout << "ERR" << endl;
18     }
19 }
20 
21 int main() {
22     foo ( "C:\\Windows" );
23     foo ( "C:\\Windows\\explorer.exe" );
24     foo ( "W:\\WWW" );
25     return 0;
26 }

 

原文链接: https://www.cnblogs.com/rms365/p/10961594.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    C++ stat判断路径是文件还是目录

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/296043

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月15日 下午5:26
下一篇 2023年2月15日 下午5:26

相关推荐