Question

So I'm trying to create a directory and I cannot figure out why its dying when I do. I've tried a hardcode test of the path so I don't think its a permission issue. When I cut apart the path I want to create, I make sure to NULL terminate the string. I've even tried to get the error with GetLastError() but it crashes the program so I can't. What have I done wrong?

EDIT:: If I uncomment the line in main, let it create the folder and delete it before I try to create it again in the function, the function succeeds. what...is this actually a permission issue somehow?

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;

}
Was it helpful?

Solution

You didn't allocate any memory for "dir".

char *dir;
dir = malloc(sizeof(char) * (size + 1));
dir = memcpy(dir, path, size + 1);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top