Create directory in C

Hello,

i try to create dir in C like that:

mkdir("/home/chercheur/1");

but when i try to open this directory: i have this message

Vous n'avez pas les permissions n�cessaires pour afficher le contenu de �*1*�.

have you an idea please
Thank you.

Despite the best efforts of my teachers throughout high school, I cannot speak French. :stuck_out_tongue: If you want anyone to understand this errror, translating it first would be a good idea.

I think I can guess what happened anyway, though. From man 2 mkdir :

MKDIR(2)                   Linux Programmer's Manual                  MKDIR(2)



NAME
       mkdir - create a directory

SYNOPSIS
       #include <sys/stat.h>
       #include <sys/types.h>

       int mkdir(const char *pathname, mode_t mode);

...

You didn't put file permissions into your mkdir call, thus created your directory with crazy permissions.

Try mkdir("/path/to/whatever", 0750); which should make it readable and writable to you, readable to your default group, and not available to anyone else.

1 Like

The fact that the code compiled means you omitted the necessary headers. You should check your man page and include them.

Also, mkdir's mode argument doesn't specify the final mode, merely the least restrictive mode allowed. If you require a specific mode for your newly-created directory, you must account for the process' creation mask. Either call chmod after mkdir or call umask before mkdir.

Regards,
Alister

You can crate directory using below code,

#include<iostream.h>
 #include<sys/stat.h>
 #include<sys/types.h>
 using namespace std;
 
 main()
 {
 
     if(mkdir("pathname",0777)==-1)//creating a directory
     {
         cerr<<"Error :  "<<strerror(errno)<<endl;
         exit(1);
     }
 
 }