好吧,在程序创建一个文件夹后,我会把文件放在该文件夹中,但我希望文件是用户放入的名称,所以我该怎么办呢,因为我已经设置了所有文件它确实是通过Exampleuser输入的名称生成一个文件并放入" / two"只需创建一个名为2的文件。
#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;
}
答案 0 :(得分:2)
旧的c ++标准要求您将const char*
参数传递给std::fstream::open()
方法。你可以写
inout.open(std::string(newFolder + "/" + two).c_str(),ios::out);
或用于Windows文件系统
inout.open(std::string(newFolder + "\\" + two).c_str(),ios::out);
答案 1 :(得分:1)
#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();