C++ stat判断路径是文件还是目录
1 1 #include <iostream> 2 2 #include <sys/stat.h> 3 3 4 4 using namespace std; 5 5 6 6 void foo ( const char* path ) { 7 7 struct stat s; 8 8 if ( stat ( path, &s ) == 0 ) { 9 9 if ( s.st_mode & S_IFDIR ) { 1010 cout << "DIR" << endl; 1111 } else if ( s.st_mode & S_IFREG ) { 1212 cout << "FILE" << endl; 1313 } else { 1414 cout << "?" << endl; 1515 } 1616 } else { 1717 cout << "ERR" << endl; 1818 } 1919 } 2020 2121 int main() { 2222 foo ( "C:\\Windows" ); 2323 foo ( "C:\\Windows\\explorer.exe" ); 2424 foo ( "W:\\WWW" ); 2525 return 0; 2626 }