我正在尝试使用 tpl 序列化包含 wchar_t* 字符串的结构。
我的代码看起来像这样,但它不起作用:
#include <stdio.h>
#include <locale.h>
#include <string.h>
#include <wchar.h>
#include "tpl.h"
struct chinese_t {
wchar_t *chars;
};
int main() {
tpl_node *tn;
struct chinese_t cstr;
cstr.chars = L"字符串";
tn = tpl_map("S(s)", &cstr);
tpl_pack( tn, 0 );
tpl_dump(tn, TPL_FILE, "string.tpl");
tpl_free(tn);
struct chinese_t cstr2;
tn = tpl_map( "S(s)", &cstr2);
//tpl_load(tn, TPL_MEM, buffer, len);
tpl_load(tn, TPL_FILE, "string.tpl");
tpl_unpack(tn, 0);
tpl_free(tn);
printf("%ls\n", cstr2.chars);
return;
}
如果我用“1234”替换中文“字符串”字符串,它只会打印“1”——如果我更改定义以便结构用户使用 char*(并且我只将 ASCII 字符推入其中),它就可以正常工作。但是我不知道如何让它正确地序列化和反序列化 wchar_t* 字符串。