c++ - 如何将char数组连接成字符串

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

下面我在一个字符数组中有两个变量存储

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/

相关文章:

c++ - lock_guard always owns the lock mode of the referenced mutex 是什么意思?

c++ - 在C++中的运行时删除数组以节省一些内存

c++ - Visual Studio 6.0 C++ GlobalMemoryStatusEx(需要旧 SDK)

VB6 连接到数据库

c++ - 使用 Visual Studio 2010 构建 Visual Studio 6 项目

c++ - 以最小的开销删除多余的参数包参数

c++ - 在 C89 和 C++ 中用空参数调用宏真的是未定义的行为吗?

c++ - 多次传递输入参数的完美转发

c++ - 从另一个 vector 搜索和查找一个 vector 中的元素的有效方法?

c++ - 与 priority_queue 中的第三个变量进行比较,c++