gpt4 book ai didi

c - 将 GetCurrentDirectory 与 wcstombs 一起使用

转载 作者:行者123 更新时间:2023-11-30 16:38:44 26 4
gpt4 key购买 nike

我从同事那里得到了一段以前写的代码,令人困惑。代码是:

TCHAR Curr_dir[100];  
char* input_file;
DWORD a = GetCurrentDirectory(100, Curr_dir);
size_t i= wcstombs(&input_file[i], Curr_dir, 100);

问题是 Curr_dir 不是 wcstombs 所需的类型。是否有其他函数可以对这种类型的变量执行 wcstombs 的操作?或者有什么办法可以转换吗?

最佳答案

看起来您的代码曾经支持多字节和 UNICODE,但在停止编译多字节后就退化了(不再支持 Windows 95)。

仅当您定义了 _UNICODE 时,这段代码才有意义。在这种情况下,它在预处理器之后结束如下:

wchar_t Curr_dir[100];  
char* input_file;
unsigned long a = GetCurrentDirectoryW(100, Curr_dir);
size_t i= wcstombs(&input_file[i], Curr_dir, 100);

在本例中,GetCurrentDirectoryWCurr_dir 中返回 UINCODE(宽字符串),并且出于某种原因将其转换为多字节字符串。并且字符的类型匹配。

但是,如果 _UNICODE 未定义,则代码将更改为以下内容:

char Curr_dir[100];  
char* input_file;
unsigned long a = GetCurrentDirectoryA(100, Curr_dir);
size_t i= wcstombs(&input_file[i], Curr_dir, 100);

并且 GetCurrentDirectoryA 现在切换到 ANSI version of the API,不再调用 wcstombs 了。

通常,MDSN 文档在通用文本例程映射下有一个表格,其中包含所有版本的字符串函数(例如 strcmp-wcscmp-mbscmp),wcstombs 没有“_t”版本,因此您可以需要使用#ifdef _UNICODE

但是,即使 Win32 API 的多字节版本可能仍受支持,继续使用它们也是没有意义的。

更多:https://www.codeproject.com/articles/76252/what-are-tchar-wchar-lpstr-lpwstr-lpctstr-etc

关于c - 将 GetCurrentDirectory 与 wcstombs 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47375398/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com