Create c++ directories with Linux

Asked

Viewed 217 times

1

I want to create a directory in the home folder with c++, but there is no way to do this cross-platform. I found that in the library "sys/stat. h" has the mkdir function that creates directories, however, in addition to the directory name, the precise function of the directory pages. so I made the code:

mkdir(nomedodiretorio.c_str(),0777);

It generates a folder, only it only gives write permission to me and not to other users. I have tried all the variations of 0777 and it does not generate a folder with permissions of everything for everyone. Which is the correct code ?

  • 1

    Attention that 0777 is interpreted as octal and actually corresponds to the number 511 decimally. Have you seen the permissions parameters in documentation ?

2 answers

2


From , it is possible to perform operations with the file system in a portable/cross-platform way, using the library <filesystem>:

#include <filesystem>
namespace fs = std::filesystem;

int main()
{
    fs::path nome_do_diretório = "exemplo1";
    fs::create_directory(nome_do_diretório);
}

If you want to create directories recursively (e. g. exemplo/de/dir/recursivo), use std::filesystem::create_directories.

If you don’t have access to , you can use the Boost equivalent library (almost the entire API is equivalent to the one introduced in C++, so it is possible to change the implementation without practically changing the code.)

A better way to pass the correct permissions to the new directory is to use the permissions of the existing directory where the new one will be created. To do this, pass the directory whose permissions will be copied as the second argument:

#include <filesystem>
namespace fs = std::filesystem;

int main()
{
    fs::path novo_dir = "exemplo1";
    fs::path existente_dir = ".";
    fs::create_directory(novo_dir, existente_dir);
}

On Linux, the copy of permissions is equivalent to the following code:

#include <filesystem>
#include <sys/stat.h>
namespace fs = std::filesystem;

int main()
{
  fs::path novo_dir = "exemplo1";
  fs::path existente_dir = ".";
  struct stat atributos;
  stat(existente_dir.c_str(), &atributos);
  mkdir(novo_dir.c_str(), atributos.st_mode);
}

0

In there is no standard way to do such a thing. However, there is a very elegant alternative to solving your problem using the library boost::filesystem, that has a function called create_directories(), which in turn is able to create directories recursively, let’s see:

#include <cstring>
#include <boost/system/error_code.hpp>
#include <boost/filesystem.hpp>

int main( void )
{
    std::string dir = "foo/bar";
    boost::system::error_code ec;

    boost::filesystem::create_directories( dir.c_str(), ec );

    if( ec )
    {
        std::cout << "Erro criando diretorio '" << dir << "': " << ec.message() << std::endl;
        return 1;
    }

    std::cout << "Diretorio '" << dir <<"' criado com sucesso!" << std::endl;
    return 0;
}

Compiling:

$ g++ -lboost_system -lboost_filesystem teste.cpp -o teste

Browser other questions tagged

You are not signed in. Login or sign up in order to post.