我想做的是提示用户输入一个文件来保存数据,我试过的是:
string save;
cout<<"Enter filename to save (ex: file.txt): ";
cin>>save;
ofstream myfile;
myfile.open(save);
myfile <<"ID \tName \tSales\n";
但是我得到一个错误:没有匹配函数来调用‘std::basic_ofstream >::open(std::string&)’
编辑:
所以我已经更新了我的代码,但这不允许我输入文件名。这是为什么
char file_name[81];
cout<<"Enter filename to save (ex: file.txt): ";
fflush(stdin);
cin.getline(file_name, 81);
最佳答案
在 C++03 中没有采用 std::string
的构造函数或 open
函数:myfile.open(save.c_str());
。但是,他们在 C++11 中添加了它们。
这是一个nice reference .注意 std::string
版本旁边的 since C++11
注释。
关于c++ - 提示用户输入一个文件来保存数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13336631/