ofstreamvariable.open支持预定的字符串变量吗?
我的 IDE 在最后一行的“文件名”变量上存在问题。有人可以告诉我为什么吗?
switch(filename_selection)
{
case 1: filename_selection = 1;
filename = "foo3.sql";
break;
case 2: filename_selection = 2;
filename = "foo2.sql";
break;
case 3: filename_selection = 3;
filename = "foo1.sql";
break;
default:
cout << "Invalid selection." << endl;
break;
}
ofstream File;
File.open(filename, ios::out | ios::trunc);
My IDE is having issues with the "filename" variable on the last line. Can someone point out to me why?
switch(filename_selection)
{
case 1: filename_selection = 1;
filename = "foo3.sql";
break;
case 2: filename_selection = 2;
filename = "foo2.sql";
break;
case 3: filename_selection = 3;
filename = "foo1.sql";
break;
default:
cout << "Invalid selection." << endl;
break;
}
ofstream File;
File.open(filename, ios::out | ios::trunc);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我的水晶球今天有点浑浊,但我想我能看到一些东西......
您的
文件名
被声明为std::string filename;
。遗憾的是,在 C++03 中,std::(i|o)fstream
类没有接受std::string
变量的构造函数,只有char const*
的。解决方案:传递
filename.c_str()
。My crystal ball is a bit cloudy today, but I think I can see something...
<psychic-powers>
Your
filename
is declared asstd::string filename;
. Sadly, in C++03,std::(i|o)fstream
classes didn't have constructors that acceptstd::string
variables, onlychar const*
ones.Solution: Pass
filename.c_str()
.</psychic-powers>
假设filename是std::string类型,那么你不能将它直接传递给ofstream构造函数:你需要c_str()的力量< /strong>
另外,您似乎误解了如何使用 switch 语句。
Assuming that filename is of type std::string, then you can't pass it directly to the ofstream constructor: you need the power of c_str()
Also you seem to have misunderstood how to use the switch statement.