zoukankan      html  css  js  c++  java
  • Mkdir

    一个 Mkdir 函数。可用于创建递归目录。

    实验环境: linux+ gcc3.2

    #include <iostream>
    #include 
    <string>
    #include 
    <sys/stat.h>
    #include 
    <sys/types.h>
    #include 
    <vector>
    using namespace std;


    int Mkdir( const char *pathname, mode_t mode )
    {
        
    char* pPath = NULL;
        vector
    < const char* > folders ;
        
    if (  ( pPath = (char*)malloc( strlen( pathname ) + 2  ) ) == NULL )
        {
            
    return -1;
        }
        strncpy( pPath, pathname,  strlen( pathname )  
    + 2   );
        
    // 补上结尾的 '/'
        pPath[strlen(pPath)] = '/';
        
    char*pPath2 = pPath;
        
    if ( *pPath2  )
        {
            folders.push_back( pPath2
    ++ ); 
        }       
        
    while ( *pPath2 )
        {
            
    if ( *pPath2 == '/' )
            {
                
    *pPath2++ = '\0';
                
    while ( *pPath2 == '/' )
                {
                    
    *pPath2++ = '\0';
                }
                folders.push_back( pPath2
    ++ );        
            }
            
    else
            {
                pPath2
    ++;
            }
        }
        
    string s;
        
    for (  int index = 0 ; index < folders.size() ; index++ )
        {
            s.append( folders[index] );
            s.append( 
    "/" );
            
    if ( mkdir( s.c_str() , mode ) == -1 && errno != EEXIST )
            {
                cout 
    <<  "error:" << strerror( errno )  << endl;
                
    return -1;
            }

        }
        delete pPath;
        
    return 0;
    }
  • 相关阅读:
    linux系统备份
    VNC轻松连接远程Linux桌面
    Cacti监控服务器配置教程(基于CentOS+Nginx+MySQL+PHP环境搭建)
    Linux tar命令高级用法——备份数据
    在linux下使用debugfs恢复rm删除的文件
    Linux系统MySQL开启远程连接
    查看LINUX进程内存占用情况
    JavaScript使用数组
    JavaScript计时器
    大话三层架构
  • 原文地址:https://www.cnblogs.com/diylab/p/1447796.html
Copyright © 2011-2022 走看看