Question

Alright after the program makes a folder I would liek to put a file in that folder but I want the file to be of a name the user would put in so how would I do that because as I have it set up now all it does is makes a file by he name of Exampleuser input and putting "/two" just makes a file by the name of two.

#include <iostream>
#include <direct.h>
#include <string>
#include <fstream>

using namespace std;

string newFolder = "Example";
string test ;
string two;


int main()
{

    _mkdir((newFolder.c_str()));

    getline(cin,test);
    two = test;


    fstream inout;
    inout.open(newFolder + two,ios::out);
    inout << " This is a test";
    inout.close();


    return 0;
}
Was it helpful?

Solution

The older standards of c++ require you pass a const char* parameter to the std::fstream::open() method. You can just write

inout.open(std::string(newFolder + "/" + two).c_str(),ios::out);

or for windows file system

inout.open(std::string(newFolder + "\\" + two).c_str(),ios::out);

OTHER TIPS

#include <iostream>
#include <fstream>
#include <string>
.
.
string filename;
cin >> filename;
string path; // the path to newfolder = "Example"
path += filename; //append filename to the path, which in  your case you should append "\\" between path and filename as well.

ofstream ofs(path); // create the file
.
.
ofs.close();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top