首页 > 编程语言 > 详细

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

时间:2019-06-02 00:12:07      阅读:236      评论:0      收藏:0      [点我收藏+]

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 }

 

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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!