好吧,在程序创建一个文件夹后,我想在该文件夹中放置一个文件,但我希望该文件具有用户输入的名称,所以我该怎么做,因为我现在已经设置好了它的作用是通过 Exampleuser 输入的名称创建一个文件,然后放置“/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;
}
最佳答案
旧的 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);
关于c++ - 如何使文件具有用户输入的名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22792110/