English 中文(简体)
您如何在 C++ 中创建文件夹? 没有路径名称, 仅是您想要创建的文件夹的名称? [重复]
原标题:How do you create a folder in C++ with no path name just the name of the folder you want to create? [duplicate]
  • 时间:2012-05-27 07:11:18
  •  标签:
  • c++
  • file
This question already has answers here:
Closed 11 years ago.

Possible Duplicate:
Creating a directory In C or C++

我想制作一个名为“ Bobthe Builder ” 的文件夹。 然后我想在它内部创建一个文本文件。 我想在不注意我的路径的情况下这样做。 我不想输入 :

ofstream out("C:/MyComputer/User/Jeff/etc/BobtheBuilder/NewFile.txt");

我希望它只是本地 到这个地区,在那里 我的可执行文件是 包含像这样:

ofstream out("/BobtheBuilder/NewFile.txt");

这是可能的吗? 是否我必须知道整个路径名称才能进行文件管理? 我觉得这是可能的, 因为您可以创建或打开一个文件, 它与程序一样, 在相同的目录中 :

ifstream inf("NewFile.txt");

或者有一条特殊关键字可以填充上一条路径, 像这样 :

ifstream inf("FILLIN/BobtheBuilder/NewFile.txt");

谢谢 谢谢

最佳回答

You can absolutely specify a relative path like "BobtheBuilder/NewFile.txt" without specifying the whole path. You would however need to create the folder first before the file. Since creating folders is platform specific and since you re on Windows, you would need to call the CreateDirectory function with "BobtheBuilder" as its parameter. The folder would then be created in the default working directory of the program which is the same folder where the executable resides. You can change this working directory using the SetCurrentDirectory function before creating the folder and file.

问题回答

要创建目录, 您可以使用 C 函数 :

int mkdir(const char *pathname, mode_t mode);

如果您可以使用, http://www.boost.org//"rel="nofollow" >Boost , 那么它就会变得更容易, 更友好:

bool create_directories(const path& p);

// usage example
boost::filesystem::create_directories("./BobtheBuilder");

正如你在提问中所提到的,你可以同时使用绝对和相对路径。这取决于你的意图。在你的案例中,你可以做到:

boost::filesystem::create_directories("./BobtheBuilder");
ofstream out("./BobtheBuilder/NewFile.txt");

根本不需要指定绝对路径 。

如果您经常需要管理路径, 启动会为路径管理提供许多有用的工具。 举例来说, 请考虑您在问题中提到的问题: 您想要获得当前目录的全部路径, 然后附加一个相对路径。 您可以很容易地做到这一点 :

 #include <boost/filesystem.hpp>
 namespace fs = boost::filesystem;
 ...
 fs::path curr_abs_path = fs::current_path();
 fs::path rel_path = "foo/bar";
 fs::path combined = (curr_abs_path /= rel_path);
 cout << combined << endl;

假设当前目录是 / tmp/ 之前的代码片断会打印 :

/tmp/foo/bar

operator/ /code> 负责附加两个路径并返回合并结果。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?