C C++实现创建目录

下面代码是C、C++可以使用的创建目录的函数及头文件,这是引用的opencv,haartraining中的一种方式。

 1 #include <direct.h>  //不同系统可能不一样,这是在windows下的名称
 2 #include <sys/stat.h>
 3 #include <sys/types.h>
 4 
 5 int icvMkDir( const char* filename )
 6 {
 7     char path[PATH_MAX];
 8     char* p;
 9     int pos;
10 
11 #ifdef _WIN32
12     struct _stat st;
13 #else /* _WIN32 */
14     struct stat st;
15     mode_t mode;
16 
17     mode = 0755;
18 #endif /* _WIN32 */
19 
20     strcpy( path, filename );
21 
22     p = path;
23     for( ; ; )
24     {
25         pos = (int)strcspn( p, "/\" );
26 
27         if( pos == (int) strlen( p ) ) break;
28         if( pos != 0 )
29         {
30             p[pos] = '';
31 
32 #ifdef _WIN32
33             if( p[pos-1] != ':' )
34             {
35                 if( _stat( path, &st ) != 0 )
36                 {
37                     if( _mkdir( path ) != 0 ) return 0;
38                 }
39             }
40 #else /* _WIN32 */
41             if( stat( path, &st ) != 0 )
42             {
43                 if( mkdir( path, mode ) != 0 ) return 0;
44             }
45 #endif /* _WIN32 */
46         }
47         
48         p[pos] = '/';
49 
50         p += pos + 1;
51     }
52 
53     return 1;
54 }
原文地址:https://www.cnblogs.com/ydxt/p/3851166.html