码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2019-06-02 01:23:25      阅读:357      评论:0      收藏:0      [点我收藏+]

标签:span   判断   cout   explorer   names   ifd   mod   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 }

 

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

标签:span   判断   cout   explorer   names   ifd   mod   stat   文件   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!