How to make a folder/directory

后端 未结 1 1350
终归单人心
终归单人心 2021-02-19 09:06

How do I make a directory/folder with c++. I\'ve tried to use the mkdir() without success. I want to write a program that cin\'s a variable and then uses this variable to create

相关标签:
1条回答
  • 2021-02-19 09:42

    You need to #include <string>, the std::string operators are defined in that header.

    The result of the expression newFolder + dir[i] is a std::string, and mkdir() takes a const char*. Change to:

    mkdir((newFolder + dir[i]).c_str());
    

    Check the return value of mkdir() to ensure success, if not use strerror(errno) to obtain the reason for failure.

    This accesses beyond the end of the array dir:

    for (int i = 0; i<=5; i++){
        mkdir(newFolder + dir[i]);
    

    there are 5 elements in dir, so legal indexes are from 0 to 4. Change to:

    for (int i = 0; i<5; i++){
        mkdir(newFolder + dir[i]);
    

    Usestd::string for newFolder, rather than char[20]:

    std::string newFolder;
    

    Then you have no concern over a folder of more than 19 characters (1 required for null terminator) being entered.

    0 讨论(0)
提交回复
热议问题