简体   繁体   English

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

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

So this program creates a folder in the program itself with is great but how would I have it save the new file in the folder it just created. 因此,该程序在程序本身中创建了一个文件夹,它很棒,但是我如何将新文件保存在刚创建的文件夹中。

#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;
}

You need to create pathname that includes the directory and filename. 您需要创建包含目录和文件名的路径名。 Since std::string provides an override for operator+ it's as easy as hot apple pie. 由于std::string提供了operator+的替代,因此就像热苹果派一样容易。 The following should help you get on your way. 以下内容将帮助您上路。

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

In case you want the newly created directory to become the current directory, you can also try adding: 如果希望新创建的目录成为当前目录,也可以尝试添加:

_chdir((newFolder.c_str())); _chdir((newFolder.c_str()));

after calling _mkdir 调用_mkdir之后

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM