How do I recursively create a folder in Win32?

前端 未结 14 1310
孤城傲影
孤城傲影 2020-12-01 14:11

I\'m trying to create a function that takes the name of a directory (C:\\foo\\bar, or ..\\foo\\bar\\..\\baz, or \\\\someserver\\foo\\bar

14条回答
  •  借酒劲吻你
    2020-12-01 14:11

    ctacke You forgot the last segment. e.g. '\aa\bb\"cc"' Following is the modification for ctacke:

    //---------------------------------------------------------------------
    int isfexist(char *fn)
    {
        struct stat stbuf;
        extern int errno;
    
        if (stat(fn, &stbuf)) {
            if (errno == ENOENT) return(0);
            else {
                printf("isfexist: stat");
                return(0);
            }
        } else {
            if (stbuf.st_mode & S_IFDIR) return(2);
            else return(1);
        }
    }
    //---------------------------------------------------------------------
    int MakeDirTree(char *path)
    {
        char *end1, *end2;
    
        if (path[0] == '\\') end1 = path + 1;       // Case '\aa\bb'
        else if (path[1] == ':' && path[2] == '\\') end1 = path + 3;    // Case 'C:\\aa\\bb'
        else end1 = path;
    
        for(;;) {
            end2 = strchr(end1, '\\');
            if (end2 == NULL) {
                // Case '\aa\bb\'
                if (*end1 == 0) break;
                // Last segment '\aa\bb\"cc"' not yet proceed
            } else *end2 = 0;
            if (isfexist(path) <= 0) mkdir(path);
            if (end2 == NULL) break;    // Last segment finished
            else {
                *end2 = '\\';
                end1 = end2 + 1;
            }
        }
    }
    

提交回复
热议问题