如何在windows和linux中使用相同的宽字符串格式
How to use same wide char string format for windows and linux
目前,我有一些代码将在Windows/Linux上运行。现在我在输出一些信息时遇到了一个问题
在windows上,我使用_vsnwprintf_s()来支持变量参数。所以它支持以下格式。
Log0(L"111");
Log1(L"%s", L"22");
Log2(L"%s, %d", L"33", 44);
在Linux上,我不能使用vswprintf来格式化字符串,但它需要使用%ls
来格式化宽字符串。
Debug0(L"111");
Debug1(L"%ls", L"22");
目前,我想包装一个统一的函数InfoX()来支持跨平台,所以它内部会根据当前的操作系统类型使用LogX()或DebugX()
如您所见,在windows上,我将使用%s格式化宽字符串,但在linux上使用%ls。我不知道如何在在Info2()函数中。
Info2(L"???", L"22");
为什么不使用stl库?它是解决你问题的更好的工具。您使用宽字符,所以std::wstring是最好的解决方案。要创建"wstring"值,您可以使用"wstringstream",并且不需要格式化字符。
#include <sstream>
#include <iostream>
void LOG(std::wstring _sLog)
{
std::wcout << _sLog << 'n';
//Your LOG code
}
int main()
{
std::wstringstream oss;
std::wstring wideValue(L"my wide value :");
oss << wideValue << 101;
std::wstring s = oss.str();
LOG(s);
return 0;
}
相关文章:
- 是否可以从格式字符串中检索"width"
- 我应该如何从 stdin C++ 中读取可变长度的格式字符串?
- 从用户定义的类生成格式字符串?
- 如何连接格式字符串?
- 如何在C++中将"%Y%m%d"格式字符串转换为time_t变量?
- CPPCHECK C++成员函数上的格式字符串
- scanf() 语句中"%*[^n]"的格式字符串指示什么?分配抑制器 (*) 和否定扫描集 ([^) 如何协同工作?
- 自定义 {fmt} 格式化函数,具有编译时格式字符串检查功能
- '|' scanf 格式字符串是什么意思
- 如何使用 msgfmt 验证格式字符串中的位置表示法占位符
- sscanf_s:格式字符串"%d"需要类型为"int *"的参数,但可变参数 4 的类型为"WORD *"
- C4477: 'fprintf' :格式字符串 '%s' 需要类型为"char *"的参数,但可变参数 1 的类型为"int *"
- 如何使用OpenSSL API从其PEM格式字符串中读取RSA公钥
- C 格式字符串宏
- C++等效于 C 的格式字符串
- 如何使用 C++ 中的 rapidjson 库从 json 格式字符串中获取值
- _vsnprintf和_vsnprintf_s的格式字符串中的可用百分比字符
- 如何将格式字符串发送到iostream
- 跨平台格式字符串
- 使用 swprintf() 打印"0x0"的格式字符串应该是什么?