如何将char数组连接为字符串

时间:2019-04-11 03:45:19

标签: c++ c++98 visual-studio-6

下面我在char数组中有两个变量存储

char one[7]  = "130319";
char two[7] =  "05A501";

我尝试用stringstream连接它们

std::ostringstream sz;
sz << one<< two;

之后,我将其转换为字符串

std::string stringh = sz.str();

然后我尝试将其合并以形成文件的路径 并在该文件中写文字

std::string start="d:/testingwinnet/json/";
std::string end= ".json";
std::string concat= start+stringh + end;

ofstream myfile(concat);

myfile << "test";
myfile.close();

我收到以下错误

error C2040: 'str' : 'class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char> >' differs in levels
of indirection from 'char *

任何想法。非常感谢

2 个答案:

答案 0 :(得分:3)

问题是您使用的是Visual Studio的 非常 旧版本。比C ++ 11标准要古老得多,后者引入了将std::string作为文件名传递到文件流的功能。

打开带有const char *的文件时,必须使用C样式字符串(std::ofstream)作为文件名。

因此,当前代码的解决方案是

ofstream myfile(concat.c_str());

答案 1 :(得分:0)

考虑查看此先前的答案以进行字符串串联How to concatenate two strings in C++?

相关问题