下面我在一个字符数组中有两个变量存储
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 *
任何想法。非常感谢
最佳答案
问题是您使用的是 非常 旧版本的 Visual Studio。比 C++11 标准要早得多,后者引入了将 std::string
作为文件名传递给文件流的能力。
在使用例如 std::ofstream
打开文件时,您必须使用 C 风格的字符串 (const char *
) 作为文件名。
因此您当前代码的解决方案是
ofstream myfile(concat.c_str());
关于c++ - 如何将char数组连接成字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55624380/