PEACE

和平

管理

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 }

 

posted on 2019-06-02 00:03  614616185  阅读(4806)  评论(0编辑  收藏  举报