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

StackOverflow https://stackoverflow.com/questions/22643722

  •  21-06-2023
  •  | 
  •  

Pregunta

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;
}
¿Fue útil?

Solución

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");

Otros consejos

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

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top