mkdir c++ function
mkdir c++ function
我需要在VS 2008中使用mkdir c ++函数,该函数接受两个参数,并且从VS 2005中已弃用。
但是,这个函数在我们的代码中使用,我需要编写一个独立的产品(只包含 mkdir 函数)来调试一些东西。
我需要导入哪些头文件?我使用了 direct.h,但是编译器抱怨该参数不接受 2 个参数(原因是该函数在 VS 2005 中已弃用)。
mkdir("C:hello",0);
如果要编写跨平台代码,可以使用boost::filesystem
例程
#include <boost/filesystem.hpp>
boost::filesystem::create_directory("dirname");
这确实增加了库依赖性,但您可能还会使用其他文件系统例程,boost::filesystem
有一些很棒的接口。
如果你只需要创建一个新目录,并且你只打算使用VS 2008,你可以使用_mkdir()
,正如其他人所指出的那样。
它已被弃用,但符合 ISO C++ _mkdir()
替换它,因此请使用该版本。您只需要调用目录名称,它的唯一参数:
#include <direct.h>
void foo()
{
_mkdir("C:\hello"); // Notice the double backslash, since backslashes
// need to be escaped
}
以下是 MSDN 的原型:
int _mkdir( const char *dirname );
我的跨平台解决方案(递归):
#include <sstream>
#include <sys/stat.h>
// for windows mkdir
#ifdef _WIN32
#include <direct.h>
#endif
namespace utils
{
/**
* Checks if a folder exists
* @param foldername path to the folder to check.
* @return true if the folder exists, false otherwise.
*/
bool folder_exists(std::string foldername)
{
struct stat st;
stat(foldername.c_str(), &st);
return st.st_mode & S_IFDIR;
}
/**
* Portable wrapper for mkdir. Internally used by mkdir()
* @param[in] path the full path of the directory to create.
* @return zero on success, otherwise -1.
*/
int _mkdir(const char *path)
{
#ifdef _WIN32
return ::_mkdir(path);
#else
#if _POSIX_C_SOURCE
return ::mkdir(path);
#else
return ::mkdir(path, 0755); // not sure if this works on mac
#endif
#endif
}
/**
* Recursive, portable wrapper for mkdir.
* @param[in] path the full path of the directory to create.
* @return zero on success, otherwise -1.
*/
int mkdir(const char *path)
{
std::string current_level = "";
std::string level;
std::stringstream ss(path);
// split path using slash as a separator
while (std::getline(ss, level, '/'))
{
current_level += level; // append folder to the current level
// create current level
if (!folder_exists(current_level) && _mkdir(current_level.c_str()) != 0)
return -1;
current_level += "/"; // don't forget to append a slash
}
return 0;
}
}
现在有_mkdir()
功能。
相关文章:
- "error: no matching function for call to"构造函数错误
- 调用专用模板时出错"no matching function for call to [...]"
- 库函数需要一个 std::function<void(void)>,如何传入类函数?
- Confusion: decltype vs std::function
- 为什么 std::function 可以作为 std::not2 的参数?
- 'max'匹配'std::function<const int &(const int &, const int &)>'无过载
- 传递给std::function template的template参数究竟代表什么
- 将带有unique_ptr的可变 lambda 传递给 const&std::function
- 绑定派生类方法C++从实例范围之外的分隔 std::function 变量调用
- "no matching function for call to 'Vector::Vector'"错误
- 如何在向量中删除 std::function<void()>?
- 不断"Attempting to reference a deleted function"
- 将函数包装器转换为 std::function
- 类型擦除的std::function与虚拟函数调用的开销
- C++ std::function 对于类 exept 的所有实例都是空的(只有 Visual2019 编译器问题)
- 如果模板没有可变参数,则 Lambda 被推导出为 std::function
- 我在 ifstream input_file(文件名)行中收到错误"no matching function to call";
- 模板规范获取'Ambiguous call to overloaded function'
- 将 lambda 表达式传递给 std::function in C++
- 为什么当我在 std::string 中打开文件名的 fstream 时出现"no matching function"错误?