Program master
Program master

Reputation: 25

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

Upvotes: 1

Views: 485

Answers (2)

skortzy
skortzy

Reputation: 169

In case you want the newly created directory to become the current directory, you can also try adding:

_chdir((newFolder.c_str()));

after calling _mkdir

Upvotes: 0

Captain Obvlious
Captain Obvlious

Reputation: 20063

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. The following should help you get on your way.

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

Upvotes: 1

Related Questions