c function to create file and path

bool NewFileName(const char* filename)
{
    size_t len;
    if (0 < (len = strlen(filename)))
    {
        char* tmpbuf, *p, c;
        tmpbuf = (char*)malloc(len + 1);
        strcpy_s(tmpbuf, len + 1, filename);
        for (p = tmpbuf; *p; ++p)
        {
            if ('\\' != *p && '/' != *p)
            {
                continue;
            }
            c = *++p;
            *p = '\0';
            if ((_access(tmpbuf, 0)) != 0)
            {
                if (0 != _mkdir(tmpbuf))
                {
                    break;
                }
            }
            *p = c;
        }
        {
            FILE* file;
            free(tmpbuf);
            if (0 == _tfopen_s(&file, filename, _T("a")))
            {
                fclose(file);
                return true;
            }
        }
    }
    return false;
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325255195&siteId=291194637