如何将此变量添加到ifstream的路径中?

时间:2012-12-23 23:25:41

标签: c++

我正在尝试追加我的路径并包含一个变量作为路径的一部分,但我收到了一个错误。

它出了什么问题?

fstream fin("E:\\Games\\maps\\" + this->MapNumber + ".map", ios::in|ios::binary|ios::ate);
  

this-> MapNumber是USHORT

     

错误:13智能感知:表达式必须具有整数或未整合的枚举类型

2 个答案:

答案 0 :(得分:2)

C ++ 中,您无法使用+连接文字字符串。您可以将+std::string一起使用来连接它们,但这不适用于整数或其他类型。您需要使用。插入和提取到将导致支持它的类型将自己表示为文本,但您可能已经从一般的 I / O 中知道了这一点。

尝试这样的事情:

std::stringstream filename;
filename << "E:\\Games\\maps\\" << this->MapNumber << ".map";

std::fstream fin(filename.str().c_str(), ios::in|ios::binary|ios::ate);

就像其他一切一样,使用你需要的东西包含首先声明它的标题。要使用std::stringstream,您需要添加<sstream>

答案 1 :(得分:0)

您不能在字符串上使用operator +而在字符串上使用其他类型,因此您可以:

选项1:将所有变量转换为字符串以附加它们

string s = string("E:\\Games\\maps\\") + string(itoa(this->MapNumber)) + string(".map");

option2:使用stringstream作为@ k-ballo解释

选项3:使用旧的C fprintf(我个人最喜欢的)

char str[100];
fprintf(str, "E:\\Games\\maps\\ %d .map", this->MapNumber);
相关问题