Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/156.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将wchar_t*转换为字符时出现的问题*_C++_Visual Studio 2010_Char_Wchar T - Fatal编程技术网

C++ 将wchar_t*转换为字符时出现的问题*

C++ 将wchar_t*转换为字符时出现的问题*,c++,visual-studio-2010,char,wchar-t,C++,Visual Studio 2010,Char,Wchar T,我需要读取Windows 7中的当前目录,该目录与当前使用的区域设置不同。因此我考虑使用GetCurrentDirectoryW(),因为它与wchar\u t*是unicode兼容的。但是,我需要使用现有的API,所以我需要将其转换为char*。为此,我使用了wcstombs()函数。然而,这种转换并没有正常进行。下面是我使用的代码: wchar_t w_currentDir[MAX_PATH + 100]; char currentDir[MAX_PATH + 100];

我需要读取Windows 7中的当前目录,该目录与当前使用的区域设置不同。因此我考虑使用GetCurrentDirectoryW(),因为它与wchar\u t*是unicode兼容的。但是,我需要使用现有的API,所以我需要将其转换为char*。为此,我使用了wcstombs()函数。然而,这种转换并没有正常进行。下面是我使用的代码:

    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);
我所在的当前目录是C:\特斯塔敌人. 转换完成后,只有完整路径的“C:\”部分正确地转换为char*。其他字符不是,它们是垃圾值。我使用的这种方法有什么问题?我怎样才能纠正这个问题


谢谢大家!

问题是没有合适的转换。宽字符可能没有规则的字符等价物(这就是为什么首先存在
wchar
。因此您应该使用
wprintf

GetCurrentDirectoryW(MAX_PATH, w_currentDir);
wprintf("%s \n", w_currentDir);

谢谢。这是否意味着我们永远不能使用wcstombs函数?否则这是针对非语言环境字符的?如果您的
wchar
字符串包含非ASCII字符,则无法进行转换。好的,这意味着当前的方法不起作用,因为路径是汉字ryt?我面临的问题是我没有合作伙伴控制API,它要求当前路径为char*。如果我使用GetCurrentDirectory,则非区域设置将被破坏。是否存在任何可能的解决方法?这不是wchar\u不存在的实际原因。wchar\u t不需要能够在同一区域设置中表示不能在char中表示的任何内容。甚至没有指定wchar\u to在不同的地区使用相同的编码。wchar\u t实际上是一种1:1的字符对代码单位表示法,因此可以简单地实现文本处理算法,而不是强迫程序员直接处理多字节表示法topic@bames53只是好奇,为什么
wchar\u t
uSE2字节,而char使用1字节(通常)?