CreateDirectory崩溃程序

时间:2014-02-10 03:24:38

标签: c crash create-directory

所以我正在尝试创建一个目录,当我这样做时,我无法弄清楚为什么它会死掉。我已经尝试过路径的硬编码测试,所以我认为它不是一个权限问题。当我分开我想要创建的路径时,我确保NULL终止字符串。我甚至试图用GetLastError()来解决错误,但它崩溃了程序,所以我不能。我做错了什么?

EDIT :: 如果我取消注释main中的行,让它创建文件夹并删除它,然后再尝试在函数中再次创建它,函数成功。什么......这实际上是一个许可问题?

int main(void) {
   int start;

   char* test = "C:\\Users\\Daniel\\Desktop\\temp\\second";
   //   CreateDirectory(test, NULL);  //this works

   fileCopy("C:\\Users\\Daniel\\Desktop\\temp\\second\\datanew.txt");

    return EXIT_SUCCESS;
}


int fileCopy(char* path){

char line[500];

FILE *new;
FILE *old;

char *old_path = "C:\\Users\\Daniel\\Desktop\\temp\\data.txt";

//"C:\\Users\\Daniel\\Desktop\\temp.txt"
old = fopen(old_path, "r");
new = fopen(path, "w");

if(old != NULL){
        if(new == NULL){

            char * last;
            last = strrchr(path, 92); //the \ character

            int size = strlen(path)-strlen(last);

            char *dir;
            dir = memcpy(dir, path, size + 1);
            dir[size] = '\0';
            CreateDirectory(dir, NULL);
            new = fopen(path, "w");
        }

}

return 0;

}

1 个答案:

答案 0 :(得分:2)

你没有为“dir”分配任何内存。

char *dir;
dir = malloc(sizeof(char) * (size + 1));
dir = memcpy(dir, path, size + 1);
相关问题