10

I am a kind of new for c++ , while working on the windows CE .net compact application

while trying to write hexa datas to a file

CString dataBlock1;
dataBlock1 = "";

CString temp;
for(int i = 0; i < rLen; i++)
{
temp.Format(L"%02X ",rec[i]);
dataBlock1 += temp;

}

std::ofstream out(file);

I am getting this error can not convert parameter 1 from wchar * to const char* on while using the below write function to write hexa datas to a file

out.write(myReader.dataBlock1.GetBuffer(),myReader.dataBlock1.GetLength());

how can we convert wchar_* to const char* to make the write function work.

Thanks.

skaffman
  • 398,947
  • 96
  • 818
  • 769
saroll
  • 217
  • 2
  • 4
  • 9
  • [How to convert 'wchar_t *' to 'const char *'](http://stackoverflow.com/questions/7835119/how-to-convert-wchar-t-to-const-char). Not only is it a duplicate question, but also using `CString`, and has the exact same title. – tenfour Nov 11 '11 at 19:25

2 Answers2

7

You can use the wcstombs function, reference here.

az4dan
  • 651
  • 2
  • 10
  • 30
1

Windows has a set of classes and functions that take wchar_t, which is text stored as UTF-16, and char, which is text stored in your ANSI character set. If you have pointer to wchar_t you either need to use an appropriate class or function that accepts wchar_t, or you need to convert the data to your ANSI character set.

In this case, you want the wchar_t variant of ofstream, wofstream.

Scott Conger
  • 133
  • 1
  • 4