0

I'm trying to create a folder using mkdir in C but it wont't work

the code won't create the folders

#include <sys/stat.h>
#include <sys/types.h>
#include <stdio.h>
#include <string.h>

int main (){
    char chemin[256];
    char name[20];
    //char fichier[100];                                                        
//  FILE *fp;                                                                   
    strcpy(chemin,"/home/Deva/Documents/prog/C/la/sds");

    mkdir(chemin,0755);
    if (mkdir(chemin,0755)==-1){
        printf("\nERROR\n");
    } else { printf("fichier creer"); }
}
klutt
  • 30,332
  • 17
  • 55
  • 95
Deva Kh
  • 11
  • 1
  • 3
  • Possible duplicate of [Creating a new directory in C](https://stackoverflow.com/questions/7430248/creating-a-new-directory-in-c) – OldProgrammer Dec 03 '18 at 19:11
  • You are actually calling the "mkdir" function twice. Did you look to see if the directory is there after running your code? It may be failing the second time because it already exists. If that's not the problem, take a look at the "perror" which should give you more info about the error you're running into. – ewindes Dec 03 '18 at 19:14

3 Answers3

3

You have to create the intermediate directories first: e.g. you must create /home/Deva before creating /home/Deva/Documents, etc.

JJF
  • 2,681
  • 2
  • 18
  • 31
1

you are passing PATH not just directory name. so please check below post would help

Recursive mkdir() system call on Unix

ShivYaragatti
  • 398
  • 2
  • 8
1

2 things:

1) You need to create the intermediate directories first.

2) You cannot create a directory twice, which you do here:

mkdir(chemin,0755);
if (mkdir(chemin,0755)==-1){
klutt
  • 30,332
  • 17
  • 55
  • 95