Pregunta

I am trying to write some data to different files for each index. i.e. The file name should change from datafile0.res to datafile99.res depending on the index passed.

I am using visual C++ 2008.

The code is:

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);

When i run this code, the following error appears:

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.

Please need your help.

Thanks in advance

¿Fue útil?

Solución

This has nothing to do with your stated goal, or your concatenation.

std::fstream::open, before C++11, requires a const char*, not an std::string.

So:

coutput.open(dir.c_str());
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top