字符串变量.open支持预定的字符串变量
Does ofstream variable.open support predetermined string variables?
我的IDE最后一行的"filename"变量有问题。有人能告诉我为什么吗?
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);
我的水晶球今天有点多云,但我想我能看到一些东西。。。
<psychic-powers>
您的filename
声明为std::string filename;
。遗憾的是,在C++03中,std::(i|o)fstream
类没有接受std::string
变量的构造函数,只有接受char const*
变量的构造函数。
解决方案:通过filename.c_str()
</psychic-powers>
假设filename的类型为std::string,则不能将其直接传递给流构造函数:您需要c_str()的能力
switch(filename_selection)
{
case 1:
//filename_selection = 1; WHAT IS THIS?
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.c_str(), // <<<
ios::out | ios::trunc);
此外,您似乎误解了如何使用switch语句。
相关文章:
- 为什么我不能在不创建字符串变量的情况下使用函数的字符串输出
- 如果用户输入无效,如何使用字符串变量-C++重复输入命令
- 从字符串变量中逐字符读取单词
- 对函数 PlaySound 使用字符串变量
- C++中的字符串变量
- 字符串变量,比如说"字符串str",可以直接复制到数组中吗?
- 我可以用字符串变量而不是它的名字创建容器吗? C++
- 字符串变量的大小
- 为什么我的字符串变量没有打印完整的字符串?
- 字符串变量未更新 c++
- 如何使用 setfill 和 setw 在字符串变量中存储十六进制值
- 在.cpp文件之间传递全局字符串变量?
- 字符串变量顺序
- 为什么可以将整数分配给字符串变量?
- 在qt创建器中调试时如何访问字符串变量的完整值?
- 将字符串变量分配给无符号字符变量
- 在 c++ 中使用字符串变量选择静态类结构,而不带对象
- C++我应该用什么来保存格式化为字符串变量(如sprintf())的文件
- 我有一个字符串变量单词= "APA"
- 将字符串变量从 bash 脚本传递到根宏