在使用mkdir()创建一个文件夹后,我如何让程序将文件和其他信息保存到新文件夹中?

After using mkdir() to create a folder how do I have the program save files and other information into the the new folder

本文关键字:文件夹 文件 程序 其他 保存 信息 创建 mkdir 一个      更新时间:2023-10-16

所以这个程序在程序本身中创建了一个文件夹,这很好,但是我如何让它将新文件保存在它刚刚创建的文件夹中呢?

#include <iostream>
#include <direct.h>
#include <string>
#include <fstream>
using namespace std;
string newFolder = "Example";

int main()
{
_mkdir((newFolder.c_str()));
fstream inout;
inout.open("hello.txt",ios::out);
inout << " This is a test";
inout.close();

    return 0;
}

您需要创建包含目录和文件名的pathname。由于std::string提供了对operator+的覆盖,因此这很容易。以下建议可以帮助你上路。

inout.open(newFolder + "/hello.txt");

如果您希望新创建的目录成为当前目录,您还可以尝试添加:

_chdir (newFolder.c_str ())),

调用_mkdir