我正在尝试将一些数据写入每个索引的不同文件中。即文件名应从datafile0.res更改为datafile99.res,具体取决于传递的索引。
我正在使用visual C ++ 2008。
代码是:
void write_data(int index) {
ofstream coutput;
// first i need to convert integer index to string. Not sure if right?
ostringstream temp;
temp<<index;
std::string s = temp.str();
std::string dir = "C:\My_Data\datafile" + s + ".res";
coutput.open(dir);
运行此代码时,会出现以下错误:
error C2664: 'void std::basic_ofstream<_Elem,_Traits>::open(const wchar_t ,std::ios_base::openmode,int)' : cannot convert parameter 1 from 'std::string' to 'const wchar_t *'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called.
请你帮忙。
提前致谢
答案 0 :(得分:2)
这与您声明的目标或连接无关。
在C ++ 11之前, std::fstream::open
需要const char*
,而不是std::string
。
所以:
coutput.open(dir.c_str());