我需要在Windows上使用C創建一個簡單的目錄樹。然而,我的代碼不創建目錄,只是打印出來,和多數民衆贊成:在C,Windows,MinGW(CodeBlocks)代碼中創建目錄不會創建目錄
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
void createDirTree(char *path)
{
char *tmp = NULL, *start = NULL, *end = NULL, *slash = NULL;
tmp = malloc(sizeof(char) * (strlen(path) + 1));
strcpy(tmp, path);
start = tmp;
end = start + strlen(path);
while (start < end)
{
char *slash = strchr(start, '/');
if (strcmp(start, ".") != 0)
{
CreateDirectory(tmp, NULL);
printf("going to make %s\n", tmp);
}
if (!slash)
{
break;
}
*slash = '/';
start = slash + 1;
}
free(tmp);
}
int main()
{
char path1[] = "./mydir/dir1/dir2/";
char path2[] = "./mydir/dir1/dir2/dir45/";
createDirTree(path1);
createDirTree(path2);
return 0;
}
第1步,始終檢查錯誤。當你調用'CreateDirectory'時,你會忽略這樣做。做到這一點,並找出哪裏出了問題。將診斷代碼添加到您的程序中,但卻無法向我們顯示輸出,這也是相當令人失望的。你可以看到。我們爲什麼要被拒絕? –