0

In the following code how do I read path1 and path2 from arguments(argv)? How do I convert argv[1] and argv[2] to wide string format so that it's possible to use them as _wrename arguments?

int main(int argc, const char *argv[])
{
  const wchar_t path1[] = L"/tmp/a";
  const wchar_t path2[] = L"/tmp/b";
  _wrename(path1, path2);
  return 0;
}
OJFord
  • 10,522
  • 8
  • 64
  • 98
hpn
  • 2,222
  • 2
  • 16
  • 23
  • There is code here for unicode/ascii converion http://stackoverflow.com/questions/4786292/converting-unicode-strings-and-vice-versa However, I don't know if that's what you need. Is `argv` supposed to be UTF-8 or something? Otherwise if path1 and path2 are ascii then just use ascii functions. – Barmak Shemirani Apr 08 '15 at 21:33
  • Are you sure you don't want to use `_rename` instead of `_wrename`? – Mats Petersson Apr 08 '15 at 21:51
  • @MatsPetersson Yes. actually path1 and path2 contain unicode characters. – hpn Apr 08 '15 at 21:53
  • @hpn: What encoding do they use? [The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)](http://www.joelonsoftware.com/articles/Unicode.html) and [What Every Programmer Absolutely, Positively Needs To Know About Encodings And Character Sets To Work With Text](http://kunststube.net/encoding/) – Mooing Duck Apr 08 '15 at 22:11

2 Answers2

0

You can use something like this:

std::wstring s2ws(const std::string& s) {
    int slength = (int)s.length() + 1;
    int len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0); 
    std::wstring r(len, L'\0');
    MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, &r[0], len);
    r.resize(r.size() - 1);
    return r;
}
songyuanyao
  • 169,198
  • 16
  • 310
  • 405
deanone
  • 109
  • 1
  • 3
0

Use mbstowcs

size_t mbstowcs (wchar_t* dest, const char* src, size_t max);

Converts multibyte string to wide-character string. It translates the multibyte sequence pointed by src to the equivalent sequence of wide-characters (which is stored in the array pointed by dest), up until either max wide characters have been translated or until a null character is encountered in the multibyte sequence src (which is also translated and stored, but not counted in the length returned by the function).

If max characters are successfully translated, the resulting string stored in dest is not null-terminated.

The behavior of this function depends on the LC_CTYPE category of the selected C locale.

Dr. Debasish Jana
  • 6,980
  • 4
  • 30
  • 69