プラットフォームに依存しない方法で C++ の現在の作業ディレクトリを変更するにはどうすればよいですか?
direct.h
Windows 互換のヘッダー ファイルunistd.h
と UNIX/POSIX 互換の を見つけました。
プラットフォームに依存しない方法で C++ の現在の作業ディレクトリを変更するにはどうすればよいですか?
direct.h
Windows 互換のヘッダー ファイルunistd.h
と UNIX/POSIX 互換の を見つけました。
C ++の場合、boost :: filesystem :: current_path(セッターとゲッターのプロトタイプ)。
Boost.Filesystemに基づくファイルシステムライブラリが標準に追加されます。
この回答で推奨されているように、 POSIXchdir
と MSを使用して作業ディレクトリを変更するためのこのクロスプラットフォーム サンプル コード。同様に、現在の作業ディレクトリを決定するために、類似のとが使用されます。_chdir
getcwd
_getcwd
これらのプラットフォームの違いは、マクロcd
とcwd
.
ドキュメントによると、 のchdir
署名は絶対または相対です。成功すると 0 を返します。に見られるように、フェッチされたパスを格納するためのバッファーが (1 つのバリアントで) 必要になるため、少し複雑になります。失敗すると NULL を返し、成功すると同じ渡されたバッファへのポインタを返します。コード サンプルでは、この返された char ポインターを直接使用します。int chdir(const char *path)
path
chdir
getcwd
char *getcwd(char *buf, size_t size)
このサンプルは @MarcD に基づいていますが、メモリ リークを修正しています。さらに、簡潔さ、依存関係のないこと、基本的な障害/エラー チェックのみを行うこと、および複数の (共通の) プラットフォームで確実に動作するように努めました。
OSX 10.11.6、Centos7、および Win10 でテストしました。OSX と Centos の場合、以前は .NETg++ changedir.cpp -o changedir
としてビルドして実行していまし./changedir <path>
た。
Win10では、でビルドしましcl.exe changedir.cpp /EHsc /nologo
た。
$ 猫 changedir.cpp
#ifdef _WIN32
#include <direct.h>
// MSDN recommends against using getcwd & chdir names
#define cwd _getcwd
#define cd _chdir
#else
#include "unistd.h"
#define cwd getcwd
#define cd chdir
#endif
#include <iostream>
char buf[4096]; // never know how much is needed
int main(int argc , char** argv) {
if (argc > 1) {
std::cout << "CWD: " << cwd(buf, sizeof buf) << std::endl;
// Change working directory and test for success
if (0 == cd(argv[1])) {
std::cout << "CWD changed to: " << cwd(buf, sizeof buf) << std::endl;
}
} else {
std::cout << "No directory provided" << std::endl;
}
return 0;
}
$ g++ changedir.c -o changedir
$ ./changedir testing
CWD: /Users/Phil
CWD changed to: /Users/Phil/testing
$ g++ changedir.c -o changedir
$ ./changedir
ディレクトリが提供されていません
$ ./changedir does_not_exist
CWD: /home/phil
$ ./changedir Music
CWD: /home/phil
CWD changed to: /home/phil/Music
$ ./ changedir /
CWD: /home/phil
CWD が次のように変更されました: /
cl.exe changedir.cpp /EHsc /nologo
changedir.cppc:\Users\Phil> changedir.exe テスト
CWD: c:\Users\Phil
CWD が次のように変更されました: c:\Users\Phil\test
注: OSX はclang
、Centos gnugcc
の背後で使用しg++
ます。
あなたchdir()
が望むことをしますか?POSIX と Windows の両方で動作します。
CまたはC++のことですか?それらは完全に異なる言語です。
Cでは、言語を定義する標準はディレクトリをカバーしていません。ディレクトリをサポートする多くのプラットフォームには、または引数chdir
を取る関数がありますが、それが存在する場合でも、それが宣言されているヘッダーは標準ではありません。議論が何を意味するかについても微妙なことがあるかもしれません(例えば、Windowsにはドライブごとのディレクトリがあります)。char*
const char*
C ++では、グーグルはとにつながりchdir
、_chdir
Boostにchdirへのインターフェースがないことを示唆しています。しかし、私はC ++を知らないので、これ以上コメントしません。
あなたがしたいchdir(2)
。プログラムにシェルの作業ディレクトリを変更させようとしている場合、できません。SOには、すでにその問題に対処している回答がたくさんあります。
C++ で現在のディレクトリを変更するための優れたクロスプラットフォームの方法は、ずっと前に @pepper_chico によって提案されました。このソリューションでは、boost::filesystem::current_path()
.
現在の作業ディレクトリを取得するには、次を使用します。
namespace fs = boost::filesystem;
fs::path cur_working_dir(fs::current_path());
現在の作業ディレクトリを設定するには、次を使用します。
namespace fs = boost::filesystem;
fs::current_path(fs::system_complete( fs::path( "new_working_directory_path" ) ));
以下は、自己完結型のヘルパー関数です。
#include "boost/filesystem/operations.hpp"
#include "boost/filesystem/path.hpp"
#include <string>
namespace fs = boost::filesystem;
fs::path get_cwd_pth()
{
return fs::current_path();
}
std::string get_cwd()
{
return get_cwd_pth().c_str();
}
void set_cwd(const fs::path& new_wd)
{
fs::current_path(fs::system_complete( new_wd));
}
void set_cwd(const std::string& new_wd)
{
set_cwd( fs::path( new_wd));
}
現在の作業ディレクトリを設定/取得する方法に関する私の完全なコード例は次のとおりです。
#include "boost/filesystem/operations.hpp"
#include "boost/filesystem/path.hpp"
#include <iostream>
namespace fs = boost::filesystem;
int main( int argc, char* argv[] )
{
fs::path full_path;
if ( argc > 1 )
{
full_path = fs::system_complete( fs::path( argv[1] ) );
}
else
{
std::cout << "Usage: tcd [path]" << std::endl;
}
if ( !fs::exists( full_path ) )
{
std::cout << "Not found: " << full_path.c_str() << std::endl;
return 1;
}
if ( !fs::is_directory( full_path ))
{
std::cout << "Provided path is not a directory: " << full_path.c_str() << std::endl;
return 1;
}
std::cout << "Old current working directory: " << boost::filesystem::current_path().c_str() << std::endl;
fs::current_path(full_path);
std::cout << "New current working directory: " << boost::filesystem::current_path().c_str() << std::endl;
return 0;
}
boost
システムにインストールされている場合は、次のコマンドを使用してこのサンプルをコンパイルできます。
g++ -o tcd app.cpp -lboost_filesystem -lboost_system