1

下面我在 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 *

任何想法。非常感谢

4

2 回答 2

3

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

const char *使用例如打开文件时,您必须使用 C 风格的字符串 ( ) 作为文件名std::ofstream

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

ofstream myfile(concat.c_str());
于 2019-04-11T04:07:09.083 回答
0

考虑查看这个关于字符串连接的先前答案如何在 C++ 中连接两个字符串?

于 2019-04-11T03:59:59.287 回答