-1

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..

  • You don't use `==` to compare strings in C, you have to use `strcmp()`. This is really basic. – Barmar May 05 '18 at 08:43
  • 2
    You need to add 1 to the size you malloc to allow room for the trailing null bytes. – Barmar May 05 '18 at 08:46
  • Why do you need to cd? Just do `mkdir /db/medicine/filename`? – Barmar May 05 '18 at 08:47
  • Is `db` really in the root of your disk? At the beginning you open `db/medicine`, not `/db/medicine`. – Barmar May 05 '18 at 08:47
  • You shouldn't create the directory in the `else` block. That just tells you that the current filename doesn't match what you're looking for, but it could be a later filename. See https://stackoverflow.com/questions/42913798/searching-array-reports-not-found-even-though-its-found/42913882#42913882 – Barmar May 05 '18 at 08:49
  • Why do you even need a loop? Just try to open `db/medicine/dirname`. If it fails, then the directory doesn't exist and you should create it. – Barmar May 05 '18 at 08:50
  • Thanks Barmar. Strcmp was better. I've made changes and they work. The folder gets created and then I close the cmd window using "& exit". – SimpleCoder May 05 '18 at 13:08

1 Answers1

0

Thanks everyone.

I found another better way to do it. The following is the new code that works smooth:

// check if directory exists
    // then make medicine directory
    char *temp1 = "C:\\pharmaCMS\\db\\medicine\\";
    char *result = malloc(sizeof(strlen(temp1) + strlen(medicine.name)));
    strcpy(result, temp1);
    strcat(result, medicine.name);

    struct stat sb;

    if (stat(result, &sb) == 0 && S_ISDIR(sb.st_mode))
    {
        printf("YES\n");
    }
    else
    {
        printf("NO\n");
        CreateDirectory("C:\\pharmaCMS", NULL);
        CreateDirectory("C:\\pharmaCMS\\db", NULL);
        CreateDirectory("C:\\pharmaCMS\\db\\medicine", NULL);
        CreateDirectory(result, NULL);
    }

I notices that if we add the full path in CreateDirectory, it doesn't create the root directories.. For creating each directory, we have to pass individual commands.