[英]Issue in Converting wchar_t* to char*
I need to read the current directory in Windows 7 which is in a different locale than the one that is currently used. 我需要在Windows 7中读取当前目录,该目录与当前使用的语言环境不同。 So I thought of using GetCurrentDirectoryW() since it is unicode compatible, with wchar_t*.
因此,我考虑使用GetCurrentDirectoryW(),因为它与wchar_t *是Unicode兼容的。 However, I need to use an existing API, so I need to convert this to char*.
但是,我需要使用现有的API,因此需要将其转换为char *。 For this purpose I used the wcstombs() function.
为此,我使用了wcstombs()函数。 However, the conversion is not happening properly.
但是,转换未正确进行。 Included below is the code that I used:
以下包括我使用的代码:
wchar_t w_currentDir[MAX_PATH + 100];
char currentDir[MAX_PATH + 100];
GetCurrentDirectoryW(MAX_PATH, w_currentDir);
wcstombs (currentDir, w_currentDir, MAX_PATH + 100);
printf("%s \n", currentDir);
The current directory that I'm in is C:\\特斯塔敌人. 我当前所在的目录是C:\\特斯塔敌人。 When the conversion is done, Only the 'C:\\' part of the full path is converted to char* properly.
转换完成后,只有完整路径的'C:\\'部分会正确转换为char *。 The other characters are not, they are junk values.
其他字符不是,它们是垃圾值。 What is the problem in this approach that I'm using?
我使用的这种方法有什么问题? How can I rectify this?
我该如何纠正?
Thank you! 谢谢!
The problem is that there is no appropriate conversion possible. 问题是没有适当的转换可能。 A wide character may not have a regular char equivalent (which is why
wchar
exists in the first place. So you should be using wprintf
: 宽字符可能没有等价的普通字符(这就是
wchar
首先存在的原因。因此,您应该使用wprintf
:
GetCurrentDirectoryW(MAX_PATH, w_currentDir);
wprintf("%s \n", w_currentDir);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.