VS 2005年から廃止されましたVS 2008でます。mkdir C++の関数を使用する必要があると私は、スタンドアロンの製品を記述する必要が
(含みますmkdir関数のみ)を使って何かをデバッグします。
インポートする必要があるヘッダファイルは何ですか?私はdirect.hを使用しましたが、引数が2つの引数を取らないとコンパイラは苦情を言います(これはVS 2005で廃止された理由です)。
mkdir("C:\hello",0);
VS 2005年から廃止されましたVS 2008でます。mkdir C++の関数を使用する必要があると私は、スタンドアロンの製品を記述する必要が
(含みます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(定数するchar * dirnameが)。
くそ、。 – Electro
@Electro、より速く入力してください:) –
今日、_mkdir()
があります。
私のクロスプラットフォームソリューション(再帰的):ninja'd
#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;
}
}
上記のコードでは、folder_exists関数に間違いがあります。 stat関数を呼び出すと、エラーの戻りコードをチェックする必要があります。 -1を返すと、エラーが発生します。 Visual Studio 2010(少なくとも)では、フォルダが存在せず、すべてのフラグが1に設定されている場合、関数は-1を返します。 私はあなたにこの編集をお勧め: int型RET = STAT(dirPath.c_str()、&st); \t \t \t リターン(RET == 0)&&(st.st_mode&S_IFDIR)真:偽; これが正しく動作します。 –
+1可能な限りクロスプラットフォームに移行してください。 – pmr
ISO C++はクロスプラットフォームではありませんか?なぜブースト依存関係を追加するのですか?私は-1や何かに行くつもりはありませんが、これは過剰です。なぜディレクトリを追加するだけのリンク時間libの依存関係を追加するのですか? Boostファイルシステムは*ヘッダファイルではありません。 –
私はシステム/コンパイラに特化しているので、ファイルシステム関連の機能を避けました。私は 'mkdir()'についてはわかりませんが、標準のISO C++として定義されている参照先を教えてください。 – Tibor