I'm trying to create directory using C in windows. There are commands which work perfectly, but I first want to "cd" in a certain directory and then create the directory.
I'm using this code:
struct dirent *de;
DIR *dr = opendir("db/medicine/");
if (dr == NULL)
{
printf("Could not open current directory" );
return 0;
}
while ((de = readdir(dr)) != NULL)
{
printf("%s\n", de->d_name);
if(medicine.name == de->d_name)
{
directory_name_hit = 1;
printf("\n\n This medicine db already exists!\n");
printf("Please use edit menu for same medicine database. Redirecting ... ");
Sleep(2000);
admin_area();
}
else
{
directory_name_hit = 0;
char *temp1 = "cd \\db\\medicine\\ & mkdir ";
char *command = malloc(sizeof(strlen("cd \\db\\medicine\\ & mkdir "+strlen(medicine.name))));
strcpy(command, temp1);
strcat(command, medicine.name);
printf(command);
system(command);
}
}
closedir(dr);
I've the following includes:
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <dirent.h>
#include <string.h>
#include <dir.h>
#include <process.h>
The following error comes when I run the code:
.
cd \db\medicine\ & mkdir adThe system cannot find the path specified.
..
cd \db\medicine\ & mkdir ad
Process returned -1073741819 (0xC0000005) execution time : 18.897 s
Press any key to continue.
Why does't system run the correct command? It should work. The path is correct and there is no sub directory with same names..