mbtowc
1文字のみを変換します。使うつもりでしたmbstowcs
か?
通常、この関数を2回呼び出します。1つ目は必要なバッファサイズを取得し、2つ目は実際にそれを変換します。
#include <cstdlib> // for mbstowcs
const char* mbs = "c:\\user";
size_t requiredSize = ::mbstowcs(NULL, mbs, 0);
wchar_t* wcs = new wchar_t[requiredSize + 1];
if(::mbstowcs(wcs, mbs, requiredSize + 1) != (size_t)(-1))
{
// Do what's needed with the wcs string
}
delete[] wcs;
mbstowcs_s
(非推奨の警告のために)使用する場合は、次のようにします。
#include <cstdlib> // also for mbstowcs_s
const char* mbs = "c:\\user";
size_t requiredSize = 0;
::mbstowcs_s(&requiredSize, NULL, 0, mbs, 0);
wchar_t* wcs = new wchar_t[requiredSize + 1];
::mbstowcs_s(&requiredSize, wcs, requiredSize + 1, mbs, requiredSize);
if(requiredSize != 0)
{
// Do what's needed with the wcs string
}
delete[] wcs;
setlocale()を介して、またはロケール引数をとるバージョンmbstowcs()
(mbstowcs_l()
またはなど)を使用して、ロケールの問題に対処していることを確認してください。mbstowcs_s_l()